-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecimalPerformance.py
executable file
·95 lines (70 loc) · 1.43 KB
/
DecimalPerformance.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 18:43:38 2021
@author: maherme
"""
#%%
# Check the memory footprint of a decimal vs a float:
from decimal import Decimal
import sys
a = 3.1415
b = Decimal('3.1415')
print(sys.getsizeof(a))
print(sys.getsizeof(b))
#%%
# Check about time performance
import time
def run_float(n=1):
for i in range(n):
a = 3.1415
def run_decimal(n=1):
for i in range(n):
a = Decimal('3.1415')
n = 10_000_000
start = time.perf_counter()
run_float(n)
end = time.perf_counter()
print('float: ', end-start)
start = time.perf_counter()
run_decimal(n)
end = time.perf_counter()
print('decimal: ', end-start)
#%%
def run_float(n=1):
a = 3.1415
for i in range(n):
a + a
def run_decimal(n=1):
a = Decimal('3.1415')
for i in range(n):
a + a
n = 10_000_000
start = time.perf_counter()
run_float(n)
end = time.perf_counter()
print('float: ', end-start)
start = time.perf_counter()
run_decimal(n)
end = time.perf_counter()
print('decimal: ', end-start)
#%%
import math
def run_float(n=1):
a = 3.1415
for i in range(n):
math.sqrt(a)
def run_decimal(n=1):
a = Decimal('3.1415')
for i in range(n):
a.sqrt()
n = 5_000_000
start = time.perf_counter()
run_float(n)
end = time.perf_counter()
print('float: ', end-start)
start = time.perf_counter()
run_decimal(n)
end = time.perf_counter()
print('decimal: ', end-start)
#%%