Created
September 22, 2022 13:49
-
-
Save akx/b7e1e833e37f71017bd7440fdef1d60d to your computer and use it in GitHub Desktop.
This file contains 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
$ hyperfine --show-output 'python3 so73815479.py' 'python3 so73815479.py multi' --max-runs 5 | |
Benchmark 1: python3 so73815479.py | |
multi_proc: False, data dur: 2.83, local dur: 2.31 | |
multi_proc: False, data dur: 2.61, local dur: 2.09 | |
multi_proc: False, data dur: 2.74, local dur: 2.22 | |
multi_proc: False, data dur: 2.75, local dur: 2.26 | |
multi_proc: False, data dur: 2.83, local dur: 2.58 | |
Time (mean ± σ): 5.180 s ± 0.256 s [User: 5.875 s, System: 0.196 s] | |
Range (min … max): 4.852 s … 5.551 s 5 runs | |
Benchmark 2: python3 so73815479.py multi | |
finish new process | |
multi_proc: True, data dur: 0.77, local dur: 2.29 | |
finish new process | |
multi_proc: True, data dur: 0.78, local dur: 2.35 | |
finish new process | |
multi_proc: True, data dur: 0.85, local dur: 2.38 | |
finish new process | |
multi_proc: True, data dur: 0.77, local dur: 2.29 | |
finish new process | |
multi_proc: True, data dur: 0.75, local dur: 2.31 | |
Time (mean ± σ): 3.262 s ± 0.073 s [User: 6.561 s, System: 0.361 s] | |
Range (min … max): 3.201 s … 3.379 s 5 runs | |
Summary | |
'python3 so73815479.py multi' ran | |
1.59 ± 0.09 times faster than 'python3 so73815479.py' |
This file contains 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
import multiprocessing as mp | |
import sys | |
import numpy as np | |
import time | |
LOCAL_ITERS = 500 | |
POWER_ITERS = 200 | |
RAND_SIZE = 200 | |
def get_sample(): | |
x = np.random.rand(RAND_SIZE, RAND_SIZE) | |
p = np.random.rand(RAND_SIZE, RAND_SIZE) | |
y = 0 | |
for i in range(POWER_ITERS): | |
y += np.power(x, p) | |
return y | |
def data_generator(q_data, total_loops): | |
for i in range(total_loops): | |
q_data.put(get_sample()) | |
print('finish new process') | |
def main(multi_proc=False): | |
total_loops = 20 | |
if multi_proc: | |
q_data = mp.Queue() | |
new_proc = mp.Process(target=data_generator, args=(q_data, total_loops)) | |
new_proc.start() | |
get_data = q_data.get | |
else: | |
get_data = get_sample | |
new_proc = None | |
q_data = None | |
get_data_durations = [] | |
local_durations = [] | |
for i in range(total_loops): | |
get_data_start = time.perf_counter() | |
y = get_data() | |
get_data_durations.append(time.perf_counter() - get_data_start) | |
local_start = time.perf_counter() | |
for j in range(LOCAL_ITERS): | |
y += np.random.rand(RAND_SIZE, RAND_SIZE) | |
local_durations.append(time.perf_counter() - local_start) | |
if multi_proc: | |
new_proc.join() | |
assert q_data.empty() | |
data_total = sum(get_data_durations) | |
local_total = sum(local_durations) | |
print(f'multi_proc: {multi_proc}, data dur: {data_total :.2f}, local dur: {local_total :.2f}') | |
if __name__ == '__main__': | |
main(multi_proc="multi" in sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment