Created
July 10, 2021 01:21
-
-
Save MLWhiz/bcd518a62d54a8b471cf4d7ca6447c41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
import time | |
import plotly.express as px | |
import plotly | |
import pandas as pd | |
def f(x): | |
time.sleep(2) | |
return x**2 | |
def runner(list_length): | |
print(f"Size of List:{list_length}") | |
t0 = time.time() | |
result1 = [f(x) for x in list(range(list_length))] | |
t1 = time.time() | |
print(f"Without multiprocessing we ran the function in {t1 - t0:0.4f} seconds") | |
time_without_multiprocessing = t1-t0 | |
t0 = time.time() | |
pool = Pool(8) | |
result2 = pool.map(f,list(range(list_length))) | |
pool.close() | |
t1 = time.time() | |
print(f"With multiprocessing we ran the function in {t1 - t0:0.4f} seconds") | |
time_with_multiprocessing = t1-t0 | |
return time_without_multiprocessing, time_with_multiprocessing | |
if __name__ == '__main__': | |
times_taken = [] | |
for i in range(1,10): | |
list_length = i | |
time_without_multiprocessing, time_with_multiprocessing = runner(list_length) | |
times_taken.append([list_length, 'No Mutiproc', time_without_multiprocessing]) | |
times_taken.append([list_length, 'Multiproc', time_with_multiprocessing]) | |
timedf = pd.DataFrame(times_taken,columns = ['list_length', 'type','time_taken']) | |
fig = px.line(timedf,x = 'list_length',y='time_taken',color='type') | |
plotly.offline.plot(fig, filename='comparison_bw_multiproc.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment