Created
January 30, 2013 09:59
-
-
Save klen/4672096 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
""" Hello GIL! | |
""" | |
from __future__ import print_function | |
import math | |
import multiprocessing | |
import threading | |
from time import time | |
def timed(func): | |
def wrapper(*args, **kwargs): | |
start = time() * 1000 | |
func(*args, **kwargs) | |
end = time() * 1000 | |
return end - start | |
return wrapper | |
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" | |
@timed | |
def serial_factorizer(nums): | |
return {n: factorize_naive(n) for n in nums} | |
def worker(nums, outdict): | |
for n in nums: | |
outdict[n] = factorize_naive(n) | |
@timed | |
def threaded_factorizer(nums, nthreads): | |
chunksize = int(math.ceil(len(nums) / float(nthreads))) | |
threads = [] | |
outs = [{} for i in range(nthreads)] | |
for i in range(nthreads): | |
t = threading.Thread( | |
target=worker, | |
args=(nums[chunksize * i:chunksize * (i + 1)], | |
outs[i])) | |
threads.append(t) | |
t.start() | |
# Wait for all threads to finish | |
for t in threads: | |
t.join() | |
# Merge all partial output dicts into a single dict and return it | |
return {k: v for out_d in outs for k, v in out_d.iteritems()} | |
@timed | |
def mp_factorizer(nums, nprocs): | |
chunksize = int(math.ceil(len(nums) / float(nprocs))) | |
procs = [] | |
outs = [{} for i in range(nprocs)] | |
for i in range(nprocs): | |
p = multiprocessing.Process( | |
target=worker, | |
args=(nums[chunksize * i:chunksize * (i + 1)], | |
outs[i])) | |
procs.append(p) | |
p.start() | |
# Wait for all worker processes to finish | |
for p in procs: | |
p.join() | |
# Merge all partial output dicts into a single dict and return it | |
return {k: v for out_d in outs for k, v in out_d.iteritems()} | |
nums = 4000 | |
print("Single: {0}".format(serial_factorizer(range(nums)))) | |
print("2 threads: {0}".format(threaded_factorizer(range(nums), 2))) | |
print("4 threads: {0}".format(threaded_factorizer(range(nums), 4))) | |
print("8 threads: {0}".format(threaded_factorizer(range(nums), 8))) | |
print("2 process: {0}".format(mp_factorizer(range(nums), 2))) | |
print("4 process: {0}".format(mp_factorizer(range(nums), 4))) | |
print("8 process: {0}".format(mp_factorizer(range(nums), 8))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment