import timeit operator from decimal import Decimal def test op op 214

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import timeit, operator
from decimal import Decimal
def test(t, op):
[op(t(214.12),t(214.12)) for x in range(100)]
def mid(ti):
res = [ ti.timeit(100) for x in range(10) ]
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)