from timeit import timeit from functools import reduce def math_1 digi

 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
from timeit import timeit
from functools import reduce
def math_1(digits):
def reduce_func(current, new_tuple):
exp, digit = new_tuple
return current + digit * (10 ** exp)
reduce(reduce_func, enumerate(reversed(digits)), 0)
def math_2(digits):
current = 0
for exp, digit in enumerate(digits):
current += digit * (10 ** exp)
def str_1(digits):
int(''.join(map(str, digits)))
def str_2(digits):
int(''.join(str(i) for i in digits))
def alt(digits):
i = 0
num = 0
while digits:
num += digits.pop() * 10 ** i
i += 1
print(timeit('math_1([1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4])', 'from __main__ import math_1', number=300000), 'math_1')
print(timeit('math_2([1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4])', 'from __main__ import math_2', number=300000), 'math_2')
print(timeit('str_1([1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4])', 'from __main__ import str_1', number=300000), 'str_1')
print(timeit('str_2([1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4])', 'from __main__ import str_2', number=300000), 'str_2')
print(timeit('alt([1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4])', 'from __main__ import alt', number=300000), 'alt')
3.7922935400056303 math_1
3.0472869820005144 math_2
1.3564981770032318 str_1
1.854133263004769 str_2
3.852395069996419 alt