Parallel¶
[ ]:
import smpl
from smpl import plot
from smpl import functions as f
import time
from smpl.parallel import *
import multiprocessing as mp
mp.cpu_count()
[ ]:
# our heavy duty test function we aim to parallelize
def long_calc(x):
for i in range(1000000):
i = i+x
i = i*x
i = i/x
i = i-x
return x*x
Non-parallelized¶
[ ]:
tic = time.perf_counter()
print([long_calc(x) for x in range(1,20)])
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[ ]:
tic = time.perf_counter()
for x in range(1,20):
print(long_calc(x))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
Auto parallelized¶
[ ]:
tic = time.perf_counter()
print(parallel(long_calc,range(1,20)))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[ ]:
tic = time.perf_counter()
for p in parallel(long_calc,range(1,20)):
print(p)
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
Manual parallelized¶
[ ]:
tic = time.perf_counter()
print(res([calc(long_calc,x) for x in range(1,20)]))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
Lambda usage¶
[ ]:
tic = time.perf_counter()
print(parallel(lambda x : ([(i+x,i*x,i/x,i-x) for i in range(1000000)] + [x*x])[-1],range(1,20)))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[ ]:
tic = time.perf_counter()
print([(lambda x : ([(i+x,i*x,i/x,i-x) for i in range(1000000)] + [x*x])[-1])(x) for x in range(1,20)])
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[ ]:
tic = time.perf_counter()
print(partitioned_parallel(lambda x : ([(i+x,i*x,i/x,i-x) for i in range(1000000)] + [x*x])[-1],range(1,20)))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[ ]: