Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import time
- import PIL.Image
- src = PIL.Image.open('1.png')
- def do_work(iters):
- for _ in range(iters):
- tmp = src.resize((640, 480))
- def check(n_threads, total_iters=96):
- per_thread = total_iters // n_threads
- print('Check for {} threads and {} iterations ({} per thread)'.format(
- n_threads, total_iters, per_thread))
- threads = [threading.Thread(target=do_work, args=(per_thread,))
- for _ in range(n_threads)]
- t0 = time.time()
- for thread in threads:
- thread.start()
- for thread in threads:
- thread.join()
- dt = time.time() - t0
- print("Total time: {:.3f}s, per thread: {:.3f}s, per iteration in thread: {:.3f}s, per iteration total: {:.3f}s".format(
- dt, dt / n_threads, dt / per_thread, dt / total_iters))
- for n_threads in range(1, 4+1):
- check(n_threads, 96)
- """
- Check for 1 threads and 96 iterations (96 per thread)
- Total time: 6.152s, per thread: 6.152s, per iteration in thread: 0.064s, per iteration total: 0.064s
- Check for 2 threads and 96 iterations (48 per thread)
- Total time: 6.664s, per thread: 3.332s, per iteration in thread: 0.139s, per iteration total: 0.069s
- Check for 3 threads and 96 iterations (32 per thread)
- Total time: 6.239s, per thread: 2.080s, per iteration in thread: 0.195s, per iteration total: 0.065s
- Check for 4 threads and 96 iterations (24 per thread)
- Total time: 6.346s, per thread: 1.587s, per iteration in thread: 0.264s, per iteration total: 0.066s
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement