import timeit import operator from decimal import Decimal def c2 op a1

 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
import timeit
import operator
from decimal import Decimal
def c2(t, op):
a1=t(214.12)
a2=t(34.123)
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:
func = (lambda: c2(t, op))
ti = timeit.Timer(func)
results.append( [tnames[t], onames[op], mid(ti)] )
from operator import itemgetter
m = 100
for result in results:
m = result[2] if result[2] < m else m
print "Data Type\tOp\tVisualisation"
for result in results:
print result[0],'\t',result[1],'\t','#'*int(m/result[2]*40)