In [10]:
import numpy as np
import pandas as pd
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(2,6)
number_of_darts.tolist()
Out[10]:
In [15]:
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_temp.append(time_temp[1]/dart)
time_serial.append(time_temp)
time_serial
Out[15]:
In [14]:
time_temp
Out[14]:
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 [50]:
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_temp.append(time_temp[1]/darts)
time_Ipar.append(time_temp)
time_Ipar
Out[50]:
In [49]:
def pi_approx_stdlib(number_of_darts):
if __name__ == '__main__':
nCPU = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=nCPU)
start_time = time()
number_of_darts_per_thread = number_of_darts/nCPU
argu_list = [number_of_darts_per_thread]* nCPU
pi_apx = pool.map_async(pi_approx, argu_list).get()
pi_apx = np.mean(pi_apx)
end_time = time()
print pi_apx
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_temp.append(time_temp[1]/dart)
time_stdlib.append(time_temp)
time_stdlib
Out[49]:
In [30]:
def to_DataFrame(data_list):
'''
convert list to pandas DataFrame
time is in second
the unit of simu_rate is [# per us]
'''
data_df = pd.DataFrame(data_list, columns=['nDarts', 'time', 'pi_approx', 'simu_rate'])
data_df.set_index('nDarts', inplace=True)
data_df['simu_rate'] *= 1e6
return data_df
In [34]:
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()
In [51]:
serial_df = to_DataFrame(time_serial)
Ipar_df = to_DataFrame(time_Ipar)
stdlib_df = to_DataFrame(time_stdlib)
In [52]:
serial_df
Out[52]:
In [53]:
Ipar_df
Out[53]:
In [35]:
number_of_darts
Out[35]:
In [36]:
darts
Out[36]:
In [47]:
[darts]*3
Out[47]:
In [43]:
a = darts.tolist()
In [42]:
darts
Out[42]:
In [45]:
a*3
Out[45]:
In [60]:
ts = Series(np.random.randn(1000), index=date_range('1/1/2000', periods=1000))
In [71]:
from pandas import Series, DataFrame
In [59]:
from pandas import date_range
In [62]:
ts = ts.cumsum()
In [64]:
ts.plot()
Out[64]:
In [66]:
plt.figure(); ax = ts.plot(style='k--', label='Series'); plt.legend()
Out[66]:
In [70]:
ax.set_title('aa')
Out[70]:
In [73]:
In [6]: df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
In [75]:
df.head()
Out[75]:
In [83]:
axA = df.A.plot()
In [79]:
import sys
In [84]:
axA.set_title('aa')
sys.stdout.flush()
In [86]:
Ipar_df
serial_df
stdlib_df
Out[86]:
In [7]:
import numpy as np
import pandas as pd
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(2,6)
number_of_darts.tolist()
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_temp.append(dart/time_temp[1])
time_serial.append(time_temp)
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)
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 = np.mean(result)
return [execution_time, pi_apx]
time_Ipar = []
for dart in number_of_darts:
time_temp = pi_approx_par(dart)
time_temp.insert(0, dart)
time_temp.append(dart/time_temp[1])
time_Ipar.append(time_temp)
def pi_approx_stdlib(number_of_darts):
if __name__ == '__main__':
nCPU = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=nCPU)
start_time = time()
number_of_darts_per_thread = number_of_darts/nCPU
argu_list = [number_of_darts_per_thread] * nCPU
pi_apx = pool.map_async(pi_approx, argu_list).get()
pi_apx = np.mean(pi_apx)
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_temp.append(dart/time_temp[1])
time_stdlib.append(time_temp)
def to_DataFrame(data_list):
'''
convert list to pandas DataFrame
time is in second
the unit of simu_rate is [# per us]
'''
data_df = pd.DataFrame(data_list, columns=['nDarts', 'time', 'pi_approx', 'simu_rate'])
data_df.set_index('nDarts', inplace=True)
return data_df
serial_df = to_DataFrame(time_serial)
Ipar_df = to_DataFrame(time_Ipar)
stdlib_df = to_DataFrame(time_stdlib)
fig, axes = plt.subplots()
serial_df.time.plot(style='r-', label='Simple')
serial_df.simu_rate.plot(secondary_y=True, style='r--')
Ipar_df.time.plot(style='g-', label='Ipcluster')
Ipar_df.simu_rate.plot(secondary_y=True, style='g--')
stdlib_df.time.plot(style='c-', label='Multiprocessing')
stdlib_df.simu_rate.plot(secondary_y=True, style='c--')
axes.set_xlabel('Darts Thrown')
axes.set_ylabel('Execution Time(seconds), solid line')
axes.set_xscale('log')
axes.set_yscale('log')
axes.right_ax.set_yscale('log')
#axes.right_ax.set_ylabel('Simulation Rate (darts/seconds), dashed line')
axes.legend()
axes.set_title('MacBook Pro with 2.4GHZ Intel Core 2 Duo', size='large')
plt.show()
In [14]:
np.logspace(1,7,10)
Out[14]:
In [16]:
dar
In [ ]: