Parallel
[1]:
import smpl
from smpl import plot
from smpl import functions as f
import time
from smpl.parallel import *
[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 3.5963 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 3.6732 seconds
Auto parallelized
[5]:
tic = time.perf_counter()
print(par(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 0.4302 seconds
[6]:
tic = time.perf_counter()
for p in par(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 0.4293 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.4389 seconds
[ ]: