-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCE302-Week07.py
executable file
·64 lines (37 loc) · 1.06 KB
/
CE302-Week07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
##############################################
# 2021_0414-AAD: Parallel Processing Example #
##############################################
#%% First coding
import multiprocessing as mp
import pandas as pd
n_cpu = mp.cpu_count()
print( f"your computer has {n_cpu} CPU's")
# %%
import numpy as np
import time
#%%
def random_squared( seed ):
np.random.seed()
random_num = np.random.randint(0,10);
return random_num ** 2
# %%
rakam = 1_000
# %%
timeSerial = pd.DataFrame(columns=["Rakam","TimeElapsed"])
if __name__ == "__main__" :
t0 = time.time()
results1 = []
for i in range( rakam ):
results1.append( random_squared(i) ) ;
t1 = time.time()
timeElapsed = round( t1-t0 , 3)
print( f"Serial Process: Time elapsed {timeElapsed} secods")
# %%
if __name__ == "__main__" :
t0 = time.time()
n_cpu = mp.cpu_count()
pool = mp.Pool( processes= 4 , )
results2 = [ pool.map( random_squared , range(rakam))]
t1 = time.time()
print( f"Parallel Process: Time elapsed {round( t1-t0 , 3)} secods")
# %%