Skip to content

Commit 168e66a

Browse files
committed
Suggested refactoring of performance script
1 parent f629131 commit 168e66a

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

python-flatten-list/flatten.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from functools import reduce
2+
from itertools import chain
3+
from operator import add, concat, iconcat
4+
5+
6+
def flatten_comprehension(matrix):
7+
return [item for row in matrix for item in row]
8+
9+
10+
def flatten_concatenation(matrix):
11+
flat = []
12+
for row in matrix:
13+
flat += row
14+
return flat
15+
16+
17+
def flatten_extend(matrix):
18+
flat = []
19+
for row in matrix:
20+
flat.extend(row)
21+
return flat
22+
23+
24+
def flatten_itertools_chain(matrix):
25+
return list(chain.from_iterable(matrix))
26+
27+
28+
def flatten_reduce_add(matrix):
29+
return reduce(add, matrix, [])
30+
31+
32+
def flatten_reduce_concat(matrix):
33+
return reduce(concat, matrix, [])
34+
35+
36+
def flatten_reduce_iconcat(matrix):
37+
return reduce(iconcat, matrix, [])
38+
39+
40+
def flatten_reduce_lambda(matrix):
41+
return list(reduce(lambda x, y: x + y, matrix, []))
42+
43+
44+
def flatten_sum(matrix):
45+
return sum(matrix, [])

python-flatten-list/performance.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,30 @@
11
from timeit import timeit
22

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
124

135
SIZE = 1000
146
TO_MS = 1000
7+
NUM = 10
158
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",
2518
]
2619

2720
matrix = [list(range(SIZE))] * SIZE
2821

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)
3424
for func in FUNCTIONS
35-
]
25+
}
3626

3727
print(f"Time to flatten a {SIZE}x{SIZE} matrix (in milliseconds):\n")
3828

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

Comments
 (0)