Parallel¶
[1]:
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()
[1]:
16
[2]:
# 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¶
[3]:
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")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 4.5479 seconds
[4]:
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")
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
Took 4.5484 seconds
Auto parallelized¶
[5]:
tic = time.perf_counter()
print(parallel(long_calc,range(1,20)))
toc = time.perf_counter()
print(f"\nTook {toc - tic:0.4f} seconds")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 1.5297 seconds
[6]:
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")
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
Took 1.6713 seconds
Manual parallelized¶
[7]:
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")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 0.6463 seconds
Lambda usage¶
[8]:
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")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 3.2064 seconds
[9]:
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")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 9.0084 seconds
[10]:
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")
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
Took 3.1676 seconds
[ ]: