vagrant precise64 home vagrant planio ipython Python default Mar 12 20

 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
47
48
49
50
51
52
53
vagrant@precise64 /home/vagrant/planio $ ipython
Python 3.3.5 (default, Mar 12 2014, 02:09:17)
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
: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)
:
: return reduce(reduce_func, enumerate(reversed(digits)), 0)
:
:
:def math_2(digits):
: current = 0
:
: for exp, digit in enumerate(digits):
: current += digit * (10 ** exp)
:
: return current
:
:
:def str_1(digits):
: return int(''.join(map(str, digits)))
:
:
:def str_2(digits):
: return int(''.join(str(i) for i in digits))
:
:--
In [2]: timeit('math_1([1, 2, 3, 4])', 'from __main__ import math_1')
Out[2]: 2.8830573849991197
In [3]: timeit('math_2([1, 2, 3, 4])', 'from __main__ import math_2')
Out[3]: 1.9720628579962067
In [4]: timeit('str_1([1, 2, 3, 4])', 'from __main__ import str_1')
Out[4]: 2.6370103959925473
In [5]: timeit('str_2([1, 2, 3, 4])', 'from __main__ import str_2')
Out[5]: 3.4009303060011007