Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import multiprocessing
- import threading
- from time import time
- def fibonacci(n, id, value):
- a, b = 0, 1
- for _ in range(1, n):
- a, b = b, a + b
- value = b
- def processes(size, spawn):
- start_time = time()
- jobs = []
- for i in range(0, spawn):
- value = 0
- process = multiprocessing.Process(target=fibonacci,
- args=(size, i, value))
- jobs.append(process)
- for j in jobs:
- j.start()
- for j in jobs:
- j.join()
- elapsed_time = time() - start_time
- cps = int(size // elapsed_time)
- print(
- "{} processes: {} calculations per second.".format(spawn, cps)
- )
- def threads(size, spawn):
- start_time = time()
- jobs = []
- for i in range(0, spawn):
- value = 0
- thread = threading.Thread(target=fibonacci(size, i, value))
- jobs.append(thread)
- for j in jobs:
- j.start()
- for j in jobs:
- j.join()
- elapsed_time = time() - start_time
- cps = int(size // elapsed_time)
- print(
- "{} threads: {} calculations per second.".format(spawn, cps)
- )
- if __name__ == "__main__":
- size = 100000
- spawn = 20
- processes(size, spawn)
- threads(size, spawn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement