Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # time_multiprocessing.py
- import time
- from multiprocessing import Pool
- def cpu_bound(number):
- return sum(i * i for i in range(number))
- def find_sums(numbers):
- for number in numbers:
- cpu_bound(number)
- def find_sums_mp(numbers):
- pool = Pool(processes=4)
- pool.map(cpu_bound, numbers)
- pool.terminate()
- def find_sums_mp_8(numbers):
- pool = Pool(processes=8)
- pool.map(cpu_bound, numbers)
- pool.terminate()
- if __name__ == "__main__":
- numbers = [1280*800*2 + x for x in range(20)]
- start_time = time.time()
- find_sums(numbers)
- duration = time.time() - start_time
- print "Duration", duration, "seconds"
- start_time = time.time()
- find_sums_mp(numbers)
- duration = time.time() - start_time
- print "Duration", duration, "seconds"
- start_time = time.time()
- find_sums_mp_8(numbers)
- duration = time.time() - start_time
- print "Duration", duration, "seconds"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement