-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_quicksort.py
35 lines (26 loc) · 1.09 KB
/
time_quicksort.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
import math
import random
import time
from multiprocessing import Pipe, Process, cpu_count
from quicksort_parallel import quicksort_parallel
def main():
test = [1000, 10000, 100000, 1000000, 10000000, 100000000]
arrays = [[random.random() for x in range(test[case])] for case in range(len(test))]
for proc_count in range(int(math.log2(cpu_count())) + 1):
for test_case in range(len(test)):
time.sleep(2)
p_pipe, c_pipe = Pipe(duplex=False)
proc = Process(target=quicksort_parallel, args=(arrays[test_case], c_pipe, proc_count, 1))
start = time.time()
proc.start()
p_pipe.recv()
end = time.time()
proc.join()
proc.close()
print(
f'Time taken for {2 ** proc_count if proc_count != 0 else 1} '
f'process(es) to sort array with {test[test_case]} '
f'members: {end - start}')
print('---------------------------------------------------------------------------------------------')
if __name__ == '__main__':
main()