|
1 | 1 | from timeit import timeit |
2 | 2 |
|
3 | | -from flatten_comprehension import flatten_comprehension |
4 | | -from flatten_concatenation import flatten_concatenation |
5 | | -from flatten_extend import flatten_extend |
6 | | -from flatten_itertool_chain import flatten_itertools_chain |
7 | | -from flatten_reduce_add import flatten_reduce_add |
8 | | -from flatten_reduce_concat import flatten_reduce_concat |
9 | | -from flatten_reduce_iconcat import flatten_reduce_iconcat |
10 | | -from flatten_reduce_lambda import flatten_reduce_lambda |
11 | | -from flatten_sum import flatten_sum |
| 3 | +import flatten # noqa |
12 | 4 |
|
13 | 5 | SIZE = 1000 |
14 | 6 | TO_MS = 1000 |
| 7 | +NUM = 10 |
15 | 8 | FUNCTIONS = [ |
16 | | - flatten_comprehension, |
17 | | - flatten_concatenation, |
18 | | - flatten_extend, |
19 | | - flatten_reduce_lambda, |
20 | | - flatten_sum, |
21 | | - flatten_itertools_chain, |
22 | | - flatten_reduce_add, |
23 | | - flatten_reduce_concat, |
24 | | - flatten_reduce_iconcat, |
| 9 | + "flatten_comprehension", |
| 10 | + "flatten_concatenation", |
| 11 | + "flatten_extend", |
| 12 | + "flatten_reduce_lambda", |
| 13 | + "flatten_sum", |
| 14 | + "flatten_itertools_chain", |
| 15 | + "flatten_reduce_add", |
| 16 | + "flatten_reduce_concat", |
| 17 | + "flatten_reduce_iconcat", |
25 | 18 | ] |
26 | 19 |
|
27 | 20 | matrix = [list(range(SIZE))] * SIZE |
28 | 21 |
|
29 | | -results = [ |
30 | | - ( |
31 | | - f"{func.__name__}()", |
32 | | - timeit(f"{func(matrix)}", globals=globals(), number=1) * TO_MS, |
33 | | - ) |
| 22 | +results = { |
| 23 | + func: timeit(f"flatten.{func}(matrix)", globals=globals(), number=NUM) |
34 | 24 | for func in FUNCTIONS |
35 | | -] |
| 25 | +} |
36 | 26 |
|
37 | 27 | print(f"Time to flatten a {SIZE}x{SIZE} matrix (in milliseconds):\n") |
38 | 28 |
|
39 | | -for func, time in sorted(results, key=lambda result: result[1]): |
40 | | - print(f"{func:.<30}{time: >7.6f} ms") |
| 29 | +for func, time in sorted(results.items(), key=lambda result: result[1]): |
| 30 | + print(f"{func + '()':.<30}{time * TO_MS / NUM:.>10.6f} ms") |
0 commit comments