Skip to content

Instantly share code, notes, and snippets.

@thearn
Created April 20, 2013 01:07
Show Gist options
  • Save thearn/5424277 to your computer and use it in GitHub Desktop.
Save thearn/5424277 to your computer and use it in GitHub Desktop.
An experiment with pprocess
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)
print
# 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
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