Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import threading
  2. import time
  3. import PIL.Image
  4.  
  5. src = PIL.Image.open('1.png')
  6.  
  7. def do_work(iters):
  8. for _ in range(iters):
  9. tmp = src.resize((640, 480))
  10.  
  11.  
  12. def check(n_threads, total_iters=96):
  13. per_thread = total_iters // n_threads
  14. print('Check for {} threads and {} iterations ({} per thread)'.format(
  15. n_threads, total_iters, per_thread))
  16. threads = [threading.Thread(target=do_work, args=(per_thread,))
  17. for _ in range(n_threads)]
  18. t0 = time.time()
  19. for thread in threads:
  20. thread.start()
  21. for thread in threads:
  22. thread.join()
  23. dt = time.time() - t0
  24. print("Total time: {:.3f}s, per thread: {:.3f}s, per iteration in thread: {:.3f}s, per iteration total: {:.3f}s".format(
  25. dt, dt / n_threads, dt / per_thread, dt / total_iters))
  26.  
  27. for n_threads in range(1, 4+1):
  28. check(n_threads, 96)
  29.  
  30. """
  31. Check for 1 threads and 96 iterations (96 per thread)
  32. Total time: 6.152s, per thread: 6.152s, per iteration in thread: 0.064s, per iteration total: 0.064s
  33. Check for 2 threads and 96 iterations (48 per thread)
  34. Total time: 6.664s, per thread: 3.332s, per iteration in thread: 0.139s, per iteration total: 0.069s
  35. Check for 3 threads and 96 iterations (32 per thread)
  36. Total time: 6.239s, per thread: 2.080s, per iteration in thread: 0.195s, per iteration total: 0.065s
  37. Check for 4 threads and 96 iterations (24 per thread)
  38. Total time: 6.346s, per thread: 1.587s, per iteration in thread: 0.264s, per iteration total: 0.066s
  39. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement