Created
January 16, 2013 19:25
-
-
Save reite/4549999 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
These are my results: | |
{1: 2.327260809430917, | |
2: 1.7694565749482591, | |
3: 1.3241503620006503, | |
4: 0.7659831585049286, | |
5: 0.786389897507167, | |
6: 0.4727930181724931, | |
7: 0.5592591157981355, | |
8: 0.5410198394120904, | |
9: 0.4966728396999276, | |
'sync': 0.002815263798666064} | |
Why is synchronous so much faster? | |
#### | |
from time import clock as time | |
from concurrent.futures import ProcessPoolExecutor | |
def factorize_naive(n): | |
""" A naive factorization method. Take integer 'n', return list of | |
factors. | |
""" | |
if n < 2: | |
return [] | |
factors = [] | |
p = 2 | |
while True: | |
if n == 1: | |
return factors | |
r = n % p | |
if r == 0: | |
factors.append(p) | |
n = n // p | |
elif p * p >= n: | |
factors.append(n) | |
return factors | |
elif p > 2: | |
# Advance in steps of 2 over odd numbers | |
p += 2 | |
else: | |
# If p == 2, get to 3 | |
p += 1 | |
assert False, "unreachable" | |
if __name__ == '__main__': | |
nums = range(1000) | |
result = {} | |
t = time() | |
map(factorize_naive, nums) | |
result['sync'] = time() - t | |
for i in xrange(1, 10): | |
print i | |
t = time() | |
with ProcessPoolExecutor(max_workers=i) as executor: | |
list(executor.map(factorize_naive, nums)) | |
result[i] = time() - t | |
from pprint import pprint | |
pprint(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment