-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.py
79 lines (65 loc) · 1.46 KB
/
bench.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cdecimal
import cProfile
import decimal
from fractions import Fraction
from gmpy2 import mpz
def bench_init_zero():
for i in range(1000000):
f = Fraction(0, 1)
# d = decimal.Decimal('0')
# c = cdecimal.Decimal('0')
# g = mpz('0')
x = f + f
# x = d + d
# x = c + c
# x = g + g
return x
def bench_init_one():
for i in range(1):
f = Fraction(1, 1)
d = decimal.Decimal('1')
c = cdecimal.Decimal('1')
g = mpz('1')
return [f, d, c, g]
def bench_add(a):
f, d, c, g = a
for i in range(1000000):
x = f + f
x = d + d
x = c + c
x = g + g
return x
def bench_add2(a):
f, d, c, g = a
for i in range(1000000):
x = f + f + f
x = d + d + d
x = c + c + c
x = g + g + g
return x
def bench_mul(a):
f, d, c, g = a
for i in range(1000000):
x = f * f
x = d * d
x = c * c
x = g * g
return x
def bench_mul2(a):
f, d, c, g = a
for i in range(1000000):
x = f * f * f
x = d * d * d
x = c * c * c
x = g * g * g
return x
if __name__ == '__main__':
cProfile.run('bench_init_zero()')
if (1 == 0):
a = bench_init_one()
cProfile.run('bench_add(a)')
cProfile.run('bench_add2(a)')
if (1 == 1):
a = bench_init_one()
cProfile.run('bench_mul(a)')
cProfile.run('bench_mul2(a)')