-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtimings.py
79 lines (63 loc) · 1.91 KB
/
timings.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 numpy as np
from cython_example_proj import array_sum, tessellate
import time
try: # python2 & python3 compatibility
xrange
except NameError:
xrange = range
def array_sum_python(A):
"""
naive array summation
"""
m, n = A.shape
result = 0
for i in xrange(n):
for j in xrange(n):
result += A[i, j]
def tessellate_python(A):
"""
turns indices of an m by n array into an 2mn by 12 array of triangle
faces, as per the STL file format.
"""
m, n = A.shape
results = np.zeros((2 * m * n, 12))
for i in xrange(m - 1):
for k in xrange(n - 1):
idx = i * n + k
this_pt = [i, k, A[i, k]]
top_right = [i, k + 1, A[i, k + 1]]
bottom_right = [i + 1, k + 1, A[i + 1, k + 1]]
bottom_left = [i + 1, k, A[i + 1, k]]
results[idx, 3:6] = top_right
results[idx, 6:9] = this_pt
results[idx, 9:] = bottom_right
results[idx + 1, 3:6] = this_pt
results[idx + 1, 6:9] = bottom_left
results[idx + 1, 9:] = bottom_right
return results
if __name__ == "__main__":
A = np.random.randn(5000, 5000)
print(59 * "-")
print("Initialized array for sum; starting comparison:")
print(59 * "-")
t = time.time()
array_sum(A)
print("cython finished :", time.time() - t, "s")
t = time.time()
A.sum()
print("numpy finished :", time.time() - t, "s")
t = time.time()
array_sum_python(A)
print("python finished :", time.time() - t, "s")
print()
A = np.random.randn(1024, 1024)
print(59 * "-")
print("Initialized array for tessellate; starting comparison:")
print(59 * "-")
t = time.time()
tessellate(A)
print("cython finished :", time.time() - t, "s")
t = time.time()
tessellate_python(A)
print("python+numpy finished :", time.time() - t, "s")
print()