ivapopova

sumHugeRange_multithreaded.py

May 3rd, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import threading
  2. import time
  3.  
  4. def add(r):
  5.   global result
  6.   res = 0
  7.   for i in r:
  8.     res += i
  9.  
  10.   result += res
  11.   threadName = threading.current_thread().name
  12.  
  13.  
  14. #################################################
  15. # Multithreaded Processing:
  16. #################################################
  17. t = time.time()
  18. result = 0
  19.  
  20. #create threads
  21. tr1 = threading.Thread(name="tr1", target=add, args=(range(50_000_00),))
  22. tr2 = threading.Thread(name="tr2", target=add, args=(range(50_000_00,100_000_00),))
  23.  
  24.  
  25. # start threads
  26. tr1.start(); tr2.start()
  27.  
  28. # wait threads to finish their job
  29. tr1.join(); tr2.join()
  30.  
  31. print("Multithreaded Processing result: ", result)
  32. print("Multithreaded Processing took:",time.time() - t,"\n")
Advertisement
Add Comment
Please, Sign In to add comment