-
Notifications
You must be signed in to change notification settings - Fork 22
/
hud_benchmark.py
144 lines (113 loc) · 4.63 KB
/
hud_benchmark.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
File to run various math benchmarks in order to determine
what is the best way to go about some math on
various versions of the Pi.
"""
import logging
import math
import random
from common_utils import fast_math
from common_utils.logger import HudLogger
from common_utils.task_timer import TaskProfiler
python_logger = logging.getLogger("hud_benchmark")
python_logger.setLevel(logging.DEBUG)
LOGGER = HudLogger(python_logger)
MAX_TEST_CALLS = 1000000
random_angles = [fast_math.wrap_degrees(random.randrange(0.0, 360.0) + ((random.randrange(0, 9) / 10))) for i in range(1, MAX_TEST_CALLS)]
random_radians = [math.radians(random.randrange(0.0, 360.0) + ((random.randrange(0, 9) / 10))) for i in range(1, MAX_TEST_CALLS)]
random_ints = [random.randrange(0, 1000) for i in range(1, MAX_TEST_CALLS)]
random_floats = [float(i) for i in random_ints]
def mult_for_list(
num: float
):
val = num * 2.0
val = num * 4.0
def div_for_list(
num: float
):
val = num / 2.0
val = num / 4.0
for attempt in range(0, 4):
LOGGER.log_info_message("Round #{}".format(attempt))
LOGGER.log_info_message(" trig::calced")
# Test trig functions
# 4th slowest on the Pi
with TaskProfiler("trig::calced"):
for rand_val in random_angles:
radians = math.radians(rand_val)
cos_rand = math.cos(radians)
sin_rant = math.sin(radians)
LOGGER.log_info_message(" trig::cached")
# 2nd Slowest on the Pi
with TaskProfiler("trig::cached"):
for rand_val in random_angles:
cos_rand = fast_math.cos(rand_val)
sin_rant = fast_math.sin(rand_val)
LOGGER.log_info_message(" degrees_to_radians::calced")
with TaskProfiler("degrees_to_radians::calced"):
for rand_val in random_angles:
radians = math.radians(rand_val)
# 3rd Slowest on the Pi
LOGGER.log_info_message(" degrees_to_radians::cached")
with TaskProfiler("degrees_to_radians::cached"):
for rand_val in random_angles:
radians = fast_math.get_radians(rand_val)
LOGGER.log_info_message(" radians_to_degrees::calced")
with TaskProfiler("radians_to_degrees::calced"):
for rand_val in random_radians:
degrees = math.degrees(rand_val)
# Slowest on the Pi
LOGGER.log_info_message(" radians_to_degrees::cached")
with TaskProfiler("radians_to_degrees::cached"):
for rand_val in random_angles:
degrees = fast_math.get_degrees(rand_val)
LOGGER.log_info_message(" mult::float_operator")
with TaskProfiler("mult::float_operator"):
for rand_val in random_floats:
radians = rand_val * 2.0
radians = rand_val * 4.0
LOGGER.log_info_message(" div::float_operator")
with TaskProfiler("div::float_operator"):
for rand_val in random_floats:
radians = rand_val / 2.0
radians = rand_val / 4.0
LOGGER.log_info_message(" mult::float_to_int_shifted")
with TaskProfiler("mult::float_to_int_shifted"):
for rand_val in random_floats:
as_int = int(rand_val)
radians = as_int << 1
radians = as_int << 2
LOGGER.log_info_message(" div::float_to_int_shifted")
with TaskProfiler("div::float_to_int_shifted"):
for rand_val in random_floats:
as_int = int(rand_val)
radians = as_int >> 1
radians = as_int >> 2
LOGGER.log_info_message(" mult::int_shifted")
with TaskProfiler("mult::int_shifted"):
for rand_val in random_ints:
radians = rand_val << 1
radians = rand_val << 2
LOGGER.log_info_message(" mult::int_operator")
with TaskProfiler("mult::int_operator"):
for rand_val in random_ints:
radians = rand_val * 2
radians = rand_val * 4
LOGGER.log_info_message(" div::int_operator")
with TaskProfiler("div::int_operator"):
for rand_val in random_ints:
radians = rand_val / 2
radians = rand_val / 4
LOGGER.log_info_message(" div::int_shifted")
with TaskProfiler("div::int_shifted"):
for rand_val in random_ints:
radians = rand_val >> 1
radians = rand_val >> 2
LOGGER.log_info_message(" mult::float_operator_list_comprehension")
with TaskProfiler("mult::float_operator_list_comprehension"):
[mult_for_list(rand_val) for rand_val in random_floats]
LOGGER.log_info_message(" div::float_operator_list_comprehension")
with TaskProfiler("div::float_operator_list_comprehension"):
[div_for_list(rand_val) for rand_val in random_floats]
TaskProfiler.log(LOGGER)
quit()