Created
June 13, 2019 17:19
-
-
Save viniciusd/91e02d91bcccaf19c5c2ca4089471589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
from multiprocessing import Process | |
from threading import Thread | |
import time | |
def f(n): | |
for i in range(n): | |
i | |
class Clock: | |
def __enter__(self): | |
self.start = time.time() | |
def __exit__(self, *args, **kwargs): | |
self.end = time.time() | |
self.elapsed = self.end-self.start | |
print(self.elapsed) | |
if __name__ == '__main__': | |
n_runs = 4 | |
with Clock(): | |
print('Sequential') | |
for i in range(n_runs): | |
f(10**8) | |
with Clock(): | |
print('Threading') | |
pool = [] | |
for i in range(n_runs): | |
pool.append(Thread(target=f, args=(10**8,))) | |
pool[i].start() | |
for i in range(n_runs): | |
pool[i].join() | |
with Clock(): | |
print('Multiprocessing') | |
pool = [] | |
for i in range(n_runs): | |
pool.append(Process(target=f, args=(10**8,))) | |
pool[i].start() | |
for i in range(n_runs): | |
pool[i].join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment