Created
April 20, 2013 01:07
-
-
Save thearn/5424277 to your computer and use it in GitHub Desktop.
An experiment with pprocess
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
import pprocess | |
import time | |
import numpy as np | |
# Define a function to parallelize: | |
def takeuptime(x): | |
"""A function to waste CPU cycles""" | |
time.sleep(0.1) | |
return x[-1] | |
n = 256 | |
samples = 64 | |
list_of_args = [np.random.randn(n) for i in xrange(samples)] | |
# Parallel computation: | |
nproc = 4 # maximum number of simultaneous processes desired | |
results = pprocess.Map() | |
parallel_function = results.manage(pprocess.MakeReusable(takeuptime)) | |
tic=time.time() | |
[parallel_function(args) for args in list_of_args]; # Start computing things | |
parallel_results = results | |
print parallel_results[:samples+1] | |
pcpu = time.time()-tic | |
print "%f s for parallel computation." % (pcpu) | |
# Serial computation: | |
tic=time.time() | |
serial_results = [takeuptime(args) for args in list_of_args] | |
print serial_results | |
scpu = time.time()-tic | |
print "%f s for traditional, serial computation." % (scpu) | |
print 100*(1 - pcpu/scpu), "percent reduction in computing time via parallel" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment