import time import timeit import random import operator from decimal i

 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
import time
import timeit
import random
import operator
from decimal import Decimal
import sys
def c2(t, op):
a1=t(214.12)
a2=t(34.123)
for x in range(100):
op(a1,a2)
op(a1,a2)
op(a1,a2)
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]
values = [ random.random()*1000 for x in range(1,100)]
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:
tmp = [ t(random.randint(1,x)) for x in range(2,12)]
func = (lambda: c2(t, op))
ti = timeit.Timer(func)
results.append( [tnames[t], onames[op], mid(ti)] )
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)