fork download
  1. import multiprocessing as mp
  2. import time
  3. import os
  4.  
  5. def square( nums , r , t1 ) :
  6. print ("square started at :")
  7. print ("%.6f" % (time.perf_counter()-t1))
  8. for n in nums :
  9. r.append(n*n)
  10. print ("square endeded at :")
  11. print ("%.6f" % (time.perf_counter()-t1))
  12.  
  13. def cube ( nums , r , t1 ) :
  14. #time.sleep(2)
  15. print ("cube started at :")
  16. print ("%.6f" % (time.perf_counter()-t1))
  17. for n in nums :
  18. r.append(n*n*n)
  19. print ("cube endeded at :")
  20. print ("%.6f" % (time.perf_counter()-t1))
  21.  
  22.  
  23. if __name__ == "__main__" :
  24. numbers = range(1,1000000)
  25. results1 = []
  26. results2 = []
  27. t1 = time.perf_counter()
  28.  
  29. # With multiprocessing :
  30. p1 = mp.Process(target = square , args = (numbers , results1 , t1))
  31. p2 = mp.Process(target = cube , args = (numbers , results2 , t1))
  32. p1.start()
  33. #time.sleep(2)
  34. p2.start()
  35. p1.join()
  36. print ("After p1.join() :")
  37. print ("%.6f" % (time.perf_counter()-t1))
  38. p2.join()
  39.  
  40. '''
  41. # Without multiprocessing :
  42. square(numbers , results1 ,t1)
  43. cube(numbers , results2 , t1)
  44.  
  45. '''
  46. print ("square + cube :")
  47. print ("%.6f" % (time.perf_counter()-t1))
  48.  
  49. # your code goes here
Success #stdin #stdout 0.33s 56752KB
stdin
Standard input is empty
stdout
square started at :
0.011400
square endeded at :
0.208859
cube started at   :
0.007489
cube endeded at   :
0.280936
After p1.join()   :
0.213874
square + cube :
0.283224