Skip to content

Commit f0b8814

Browse files
committed
addded tests
1 parent 9a3baf0 commit f0b8814

5 files changed

Lines changed: 63 additions & 24 deletions

File tree

performance/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
# Run each test module with each executable
1818
for test_module in test_modules:
19+
print(f'{test_module.name}')
20+
1921
for bin_path in BINS:
2022
is_ft = '314t' in str(bin_path)
21-
print(f'{test_module.name}: {bin_path.name}{"t" if is_ft else ""}')
23+
print(f' {bin_path.name}{"t" if is_ft else ""}')
2224

2325
try:
2426
result = subprocess.run(

performance/np_common.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from concurrent.futures import ThreadPoolExecutor
2+
import numpy as np
3+
from conditional_futures import ConditionalThreadPoolExecutor
4+
import timeit
5+
6+
7+
def proc_single(func, array, max_workers):
8+
_ = np.fromiter((func(row) for row in array), dtype=float, count=array.shape[0])
9+
10+
def proc_thread_pool(func, array, max_workers):
11+
with ThreadPoolExecutor(max_workers=max_workers) as ex:
12+
_ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
13+
14+
def proc_cond_thread_pool(func, array, max_workers):
15+
with ConditionalThreadPoolExecutor(max_workers=max_workers) as ex:
16+
_ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
17+
18+
19+
def main(func, array, max_workers, number):
20+
for proc in (proc_single, proc_thread_pool, proc_cond_thread_pool):
21+
result = timeit.timeit(f'proc(func, array, max_workers)', globals=locals(), number=number)
22+
print(' ' * 8, proc.__name__.ljust(24), round(result / number, 6))

performance/test_np01.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
1-
import timeit
2-
from concurrent.futures import ThreadPoolExecutor
3-
41
import numpy as np
2+
from np_common import main
53

6-
from conditional_futures import ConditionalThreadPoolExecutor
7-
8-
NUMBER = 10
4+
NUMBER = 4
95
MAX_WORKERS = 2
106

117
# array = np.arange(100_000_000).reshape(1_000, 100_000)
128
array = np.arange(100_000_000).reshape(100_000, 1_000)
139
func = lambda row: (row[row % 2 == 0] ** 2).sum()
1410

1511

16-
def proc_single():
17-
_ = np.fromiter((func(row) for row in array), dtype=float, count=array.shape[0])
18-
19-
20-
def proc_thread_pool():
21-
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
22-
_ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
23-
24-
25-
def proc_cond_thread_pool():
26-
with ConditionalThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
27-
_ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
28-
29-
3012
if __name__ == '__main__':
31-
for proc in (proc_single, proc_thread_pool, proc_cond_thread_pool):
32-
result = timeit.timeit(f'proc()', globals=locals(), number=NUMBER)
33-
print('\t', proc.__name__.ljust(24), round(result, 6))
13+
main(func, array, MAX_WORKERS, NUMBER)

performance/test_np02.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
from np_common import main
3+
4+
NUMBER = 4
5+
MAX_WORKERS = 2
6+
7+
array = np.arange(100_000_000).reshape(100_000, 1_000)
8+
9+
# Shannon entropy
10+
def func(row):
11+
p = row / row.sum()
12+
return -(p * np.log(p + 1e-12)).sum()
13+
14+
if __name__ == '__main__':
15+
main(func, array, MAX_WORKERS, NUMBER)

performance/test_np03.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
from np_common import main
3+
4+
NUMBER = 1
5+
MAX_WORKERS = 8
6+
7+
array = np.arange(100_000_000).reshape(100_000, 1_000)
8+
9+
def func(row):
10+
s = 0.0
11+
c = 0.0
12+
for x in row:
13+
y = x - c
14+
t = s + y
15+
c = (t - s) - y
16+
s = t
17+
return s
18+
19+
if __name__ == '__main__':
20+
main(func, array, MAX_WORKERS, NUMBER)

0 commit comments

Comments
 (0)