In [1]:
import numpy as np
from numpy import sqrt
from random import uniform
from time import time
import multiprocessing
import matplotlib.pyplot as plt
%matplotlib
number_of_darts = 10**np.arange(1,7)
number_of_darts.tolist()


Using matplotlib backend: MacOSX
Out[1]:
[10, 100, 1000, 10000, 100000, 1000000]

In [8]:
def pi_approx_serial(number_of_darts):
    ''' approximate pi using serial method'''
    number_of_darts_in_circle = 0
    start_time = time()
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = np.sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = 4*number_of_darts_in_circle/float(number_of_darts)
    return [execution_time, pi_apx]

time_serial = []
for dart in number_of_darts:
    time_temp = pi_approx_serial(dart)
    time_temp.insert(0, dart)
    time_serial.append(time_temp)
time_serial = np.array(time_serial)

time_serial


Out[8]:
array([[  1.00000000e+01,   1.59025192e-04,   3.60000000e+00],
       [  1.00000000e+02,   1.09696388e-03,   2.60000000e+00],
       [  1.00000000e+03,   3.01859379e-02,   3.19600000e+00],
       [  1.00000000e+04,   1.30022049e-01,   3.14280000e+00],
       [  1.00000000e+05,   1.15733290e+00,   3.14216000e+00],
       [  1.00000000e+06,   1.11299388e+01,   3.13947200e+00]])

In [2]:
def pi_approx(number_of_darts):
    ''' approximate pi using MC method'''
    number_of_darts_in_circle = 0
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    return 4*number_of_darts_in_circle/float(number_of_darts)

In [14]:
from IPython import parallel
rc = parallel.Client()
dview = rc[:]
with dview.sync_imports():
    from numpy import sqrt
    from random import uniform
    from time import time
    
def pi_approx_par(number_of_darts):
    ''' approximate pi using parallel method '''
    number_of_darts_per_thread = number_of_darts/len(dview)
    start_time = time()
    d_result = dview.apply_async(pi_approx, number_of_darts_per_thread)
    result = d_result.get()
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = sum(result)/float(len(result))
    return [execution_time, pi_apx]
    

time_Ipar = []
for darts in number_of_darts:
    time_temp = pi_approx_par(darts)
    time_temp.insert(0, darts)
    time_Ipar.append(time_temp)
time_Ipar = np.array(time_Ipar)
time_Ipar


importing sqrt from numpy on engine(s)
importing uniform from random on engine(s)
importing time from time on engine(s)
Out[14]:
array([[  1.00000000e+01,   1.25110149e-02,   3.20000000e+00],
       [  1.00000000e+02,   1.12540722e-02,   2.80000000e+00],
       [  1.00000000e+03,   1.79550648e-02,   3.16000000e+00],
       [  1.00000000e+04,   8.06760788e-02,   3.12480000e+00],
       [  1.00000000e+05,   7.52233028e-01,   3.14552000e+00],
       [  1.00000000e+06,   8.04435897e+00,   3.14304800e+00]])

In [6]:
def pi_approx_stdlib(number_of_darts):
    if __name__ == '__main__':
        pool = multiprocessing.Pool(processes=2)
        start_time = time()
        pi_apx = pool.apply_async(pi_approx, [number_of_darts]).get()
        end_time = time()
        execution_time = end_time-start_time
        pool.terminate()
        return [execution_time, pi_apx]
time_stdlib = []
for dart in number_of_darts:
    time_temp = pi_approx_stdlib(dart)
    time_temp.insert(0, dart)
    time_stdlib.append(time_temp)
time_stdlib = np.array(time_stdlib)
time_stdlib


Out[6]:
array([[  1.00000000e+01,   2.32219696e-03,   4.00000000e+00],
       [  1.00000000e+02,   5.82718849e-03,   3.56000000e+00],
       [  1.00000000e+03,   1.28152370e-02,   3.16000000e+00],
       [  1.00000000e+04,   1.03483915e-01,   3.16920000e+00],
       [  1.00000000e+05,   1.14927220e+00,   3.13992000e+00],
       [  1.00000000e+06,   9.99018693e+00,   3.13997600e+00]])

In [18]:
f0, ax0 = plt.subplots()
ax0.loglog(time_serial[:,0], time_serial[:,1], label='serial')
ax0.loglog(time_Ipar[:,0], time_Ipar[:,1], label='Ipython')
ax0.loglog(time_stdlib[:,0],time_stdlib[:,1], label='StdLib')
ax0.legend()
ax0.


Out[18]:
<matplotlib.legend.Legend at 0x104ef0fd0>

In [ ]: