-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
70 lines (58 loc) · 2.65 KB
/
demo.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
65
66
67
68
69
70
from subinterpreter_parallelism import parallel
from pure_cpp_parallelism import c_factorial
from random import randint
import isolated_benchmark
from multiprocessing import Process
from threading import Thread
import logging
import time
logging.basicConfig(
format='[%(asctime)s.%(msecs)03d] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Number of task runs to spawn.
NUM_TASKS = 10
# Increase MIN & MAX to increase time taken by each task (should be < 1000000000)
MIN = 90000000
MAX = 90000100
# Benchmark Python function against multiple Python processes.
logging.warning(f"Multi-processing mode started for {NUM_TASKS} regular Python tasks")
start = time.time()
processes = [Process(target=isolated_benchmark.py_factorial, args=(randint(MIN, MAX),)) for _ in range(NUM_TASKS)]
for p in processes:
p.start()
for p in processes:
p.join()
end = time.time()
logging.warning(f"Multi-processing mode ended for {NUM_TASKS} regular Python tasks in {end-start} seconds")
# Benchmark Python function against the custom handwritten sub-interpreter parallelism module.
logging.warning(f"Sub-interpreter parallelism mode started for {NUM_TASKS} regular Python tasks")
start = time.time()
result = parallel(*[['isolated_benchmark', 'py_factorial', (randint(MIN,MAX),)] for _ in range(NUM_TASKS)])
end = time.time()
logging.warning(f"Sub-interpreter parallelism mode ended with {result=} in {end-start} seconds")
# Benchmark similar C++ extension function.
logging.warning(f"Multi-threaded C++ code execution from Python function started for {NUM_TASKS} tasks")
start = time.time()
threads = [Thread(target=c_factorial, args=(randint(MIN,MAX),)) for _ in range(NUM_TASKS)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
end = time.time()
logging.warning(f"Multi-threaded C++ code execution from Python function in {end-start} seconds")
# Benchmark Python function against a single thread.
start = time.time()
logging.warning("Single threaded mode for 1 regular Python task begins")
result = isolated_benchmark.py_factorial(randint(MIN, MAX))
end = time.time()
logging.warning(f"Single threaded mode ended for 1 regular Python task with {result=} in {end-start} seconds")
# Benchmark Python function against multiple threads.
logging.warning(f"Multi-threaded mode for {NUM_TASKS} regular Python functions begins")
start = time.time()
threads = [Thread(target=isolated_benchmark.py_factorial, args=(randint(MIN,MAX),)) for _ in range(NUM_TASKS)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
end = time.time()
logging.warning(f"Multi-threaded mode ended for {NUM_TASKS} regular Python functions in {end-start} seconds")