import timeit operator from decimal import Decimal def test op a1 a2 2

 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
import timeit, operator
from decimal import Decimal
def test(t, op):
a1 = a2 = t(214.12)
for x in range(100):
op(a1,a2)
def mid(ti):
res = []
for x in range(10):
res.append(ti.timeit(100))
return sum(res)/len(res)
ops = [operator.add, operator.sub, operator.mul, operator.div]
types = [int, long, float, complex]
results = []
tnames = {int:'int ', long:'long ',float:'float ',
Decimal:'decimal', complex:'complex'}
onames = {operator.add:'+', operator.sub:'-', operator.mul:'*',
operator.div:'/'}
print('Working...')
for t in types:
for op in ops:
ti = timeit.Timer((lambda: test(t, op)))
results.append( [tnames[t], onames[op], mid(ti)] )
m = min(results, key=lambda x: x[2])[2]
print "Data Type\tOp\tVisualisation"
for result in results:
print result[0],'\t',result[1],'\t','#'*int(m/result[2]*40)