-
Notifications
You must be signed in to change notification settings - Fork 29
/
PyGLM_vs_NumPy.py
251 lines (170 loc) · 6.88 KB
/
PyGLM_vs_NumPy.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import timeit, sys, random
import glm, numpy
ON_WINDOWS = sys.platform == 'win32'
print_horizontal_rule = lambda: print("+----------------------------------------+------------+------------+-----------+")
pad_with_spaces = lambda text, length, align="left": text + " " * (length - len(text)) if align == "left" else " " * (length - len(text)) + text
seconds_to_milliseconds = lambda seconds: int(round(seconds * 1000, 0))
pyglm_total_time = 0
numpy_total_time = 0
def word_wrap(text, max_length):
out = []
current_word = []
current_length = 0
for c in text:
if current_length >= max_length:
out.append("\n")
current_length = len(current_word)
if c == " ":
out.append("".join(current_word))
if (current_length < max_length):
out.append(" ")
current_word = []
elif c == "\n":
out.append("".join(current_word))
out.append("\n")
current_word = []
current_length = 0
else:
current_word.append(c)
current_length += 1
if current_word:
out.append("".join(current_word))
return "".join(out).rstrip()
def print_row(descr, pyglm, numpy, ratio, print_header=True, end="\n"):
descr = word_wrap(descr, 38)
descr_lines = descr.split("\n")
if print_header:
for line in descr_lines[:-1]:
print(f"| {pad_with_spaces(line, 38)} | {' ' * 10} | {' ' * 10} | {' ' * 9} |")
descr_last_line = descr_lines[-1]
print(f"| {pad_with_spaces(descr_last_line, 38)} | {pad_with_spaces(pyglm, 10, align='right')} | {pad_with_spaces(numpy, 10, align='right')} | {pad_with_spaces(ratio, 9, align='right')} |", end=end)
def run_test(descr, pyglm_setup_code, pyglm_code, numpy_setup_code, numpy_code, number):
global pyglm_total_time, numpy_total_time
descr += f"\n({number:,} times)"
if ON_WINDOWS:
print_row(descr, "", "", "", end="\r")
pyglm_result = 2**31
numpy_result = 2**31
for i in range(10):
run_pyglm_first = random.choice((True,False))
if run_pyglm_first:
pyglm_result = min(pyglm_result, timeit.timeit(pyglm_code, pyglm_setup_code, number=number))
numpy_result = min(numpy_result, timeit.timeit(numpy_code, numpy_setup_code, number=number))
else:
numpy_result = min(numpy_result, timeit.timeit(numpy_code, numpy_setup_code, number=number))
pyglm_result = min(pyglm_result, timeit.timeit(pyglm_code, pyglm_setup_code, number=number))
if ON_WINDOWS:
print_row(descr, "{}ms".format(seconds_to_milliseconds(pyglm_result)), "{}ms".format(seconds_to_milliseconds(numpy_result)), "{:.02f}x".format(numpy_result / pyglm_result), end="\r", print_header=False)
pyglm_total_time += pyglm_result
numpy_total_time += numpy_result
print_row(descr, "{}ms".format(seconds_to_milliseconds(pyglm_result)), "{}ms".format(seconds_to_milliseconds(numpy_result)), "{:.02f}x".format(numpy_result / pyglm_result), print_header=not ON_WINDOWS)
print_horizontal_rule()
print(f"""Evaluating performance of PyGLM compared to NumPy.
Running on platform '{sys.platform}'.
Python version:
{sys.version}
Comparing the following module versions:
{glm.version}
vs
NumPy version {numpy.__version__}
________________________________________________________________________________
The following table shows information about a task to be achieved and the time
it took when using the given module. Lower time is better.
Each task is repeated ten times per module, only showing the best (i.e. lowest)
value.
""")
print_horizontal_rule()
print_row("Description", "PyGLM time", "NumPy time", "ratio")
print_horizontal_rule()
############################
# Actual tests start here: #
############################
run_test("3 component vector creation",
"import glm",
"glm.vec3()",
"import numpy",
"numpy.zeros((3,), numpy.float32)",
100000
)
run_test("3 component vector creation with custom components",
"import glm",
"glm.vec3(1,2,3)",
"import numpy",
"numpy.array((1,2,3), numpy.float32)",
50000
)
run_test("dot product",
"import glm; v1 = glm.vec3(); v2 = glm.vec3()",
"glm.dot(v1, v2)",
"import numpy; v1 = numpy.zeros((3,), numpy.float32); v2 = numpy.zeros((3,), numpy.float32)",
"numpy.dot(v1, v2)",
50000
)
run_test("cross product",
"import glm; v1 = glm.vec3(1); v2 = glm.vec3(1,2,3)",
"glm.cross(v1, v2)",
"import numpy; v1 = numpy.array((1,1,1), numpy.float32); v2 = numpy.array((1,2,3), numpy.float32)",
"numpy.cross(v1, v2)",
25000
)
run_test("L2-Norm of 3 component vector",
"import glm; v = glm.vec3(1,2,3)",
"glm.l2Norm(v)",
"import numpy; v = numpy.array((1,1,1), numpy.float32)",
"numpy.linalg.norm(v)",
100000
)
run_test("4x4 matrix creation",
"import glm",
"glm.mat4(0)",
"import numpy",
"numpy.zeros((4,4), numpy.float32)",
50000
)
run_test("4x4 identity matrix creation",
"import glm",
"glm.mat4()",
"import numpy",
"numpy.identity(4, numpy.float32)",
100000
)
run_test("4x4 matrix transposition",
"import glm; m = glm.mat4()",
"glm.transpose(m)",
"import numpy; m = numpy.identity(4, numpy.float32)",
"numpy.transpose(m)",
50000
)
run_test("4x4 multiplicative inverse",
"import glm; m = glm.mat4()",
"glm.inverse(m)",
"import numpy; m = numpy.identity(4, numpy.float32)",
"numpy.linalg.inv(m)",
50000
)
run_test("3 component vector addition",
"import glm; v1 = glm.vec3(1); v2 = glm.vec3(1,2,3)",
"v1 + v2",
"import numpy; v1 = numpy.array((1,1,1), numpy.float32); v2 = numpy.array((1,2,3), numpy.float32)",
"v1 + v2",
100000
)
run_test("4x4 matrix multiplication",
"import glm; m1 = glm.mat4(); m2 = glm.mat4(2)",
"m1 * m2",
"import numpy; m1 = numpy.identity(4, numpy.float32); m2 = numpy.identity(4, numpy.float32) * 2",
"m1 * m2",
100000
)
run_test("4x4 matrix x vector multiplication",
"import glm; m = glm.mat4(); v = glm.vec4()",
"m * v",
"import numpy; m = numpy.identity(4, numpy.float32); v = numpy.zeros((4,), numpy.float32)",
"m * v",
100000
)
print_row("TOTAL",
"{:.02f}s".format(pyglm_total_time),
"{:.02f}s".format(numpy_total_time),
"{:.02f}x".format(numpy_total_time / pyglm_total_time))
print_horizontal_rule()