-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeit_test.py
91 lines (67 loc) · 2.42 KB
/
timeit_test.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
import timeit
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view as window
# SHAPE = (100, 100)
# RULES = {(1, 2): 1, (1, 3): 1, (0, 3): 1}
# NEXT_STATE = np.vectorize(lambda x, y: RULES.get((x, y), 0))
# c = np.random.randint(0, 2, SHAPE, dtype="uint8")
# n = np.random.randint(0, 9, SHAPE, dtype="uint8")
# def next_state(s, n):
# return (n < 4) * (1 - s * (n % 2 - 1) + n) // 4
# t1 = timeit.timeit(
# "NEXT_STATE(c, n)",
# number=1000,
# globals=globals()
# )
# t2 = timeit.timeit(
# "next_state(c, n)",
# number=1000,
# globals=globals()
# )
# print(f"vectorized dict: {t1:.2f} s")
# print(f"formula: {t2:.2f} s")
# c = np.random.randint(0, 2, SHAPE, dtype="uint8")
# n = np.random.randint(0, 9, SHAPE, dtype="uint8")
# print(np.array_equal(NEXT_STATE(c, n), next_state(c, n)))
class PadTest:
def __init__(self, x: np.ndarray, p: str, b=False):
self.x = x
self.z = np.zeros(shape=(102, 102), dtype="uint8")
self.idxs = [-1, *range(self.x.shape[0]), 0]
self.mode = "constant" if p == "z" else "wrap"
self.pad = self.built_in if b else self.zero if p == "z" else self.wrap
@property
def zero(self) -> np.ndarray:
self.z[1:-1, 1:-1] = self.x
return self.z
@property
def wrap(self) -> np.ndarray:
return self.x[:, self.idxs][self.idxs, :]
@property
def built_in(self) -> np.ndarray:
return np.pad(self.x, 1, mode=self.mode)
def run(self):
for _ in range(1000):
n = window(self.pad, (3, 3)).sum(axis=(2, 3)) - self.x
self.x = (n < 4) * (1 - self.x * (n % 2 - 1) + n) // 4
x = np.random.randint(2, size=(100, 100), dtype="uint8")
t1 = timeit.timeit(stmt="p.run()",
setup="p = PadTest(x, 'z')",
number=10,
globals=globals())
t2 = timeit.timeit(stmt="p.run()",
setup="p = PadTest(x, 'w')",
number=10,
globals=globals())
t3 = timeit.timeit(stmt="p.run()",
setup="p = PadTest(x, 'z', True)",
number=10,
globals=globals())
t4 = timeit.timeit(stmt="p.run()",
setup="p = PadTest(x, 'w', True)",
number=10,
globals=globals())
print(f"zero: {t1:.4f} s")
print(f"wrap: {t2:.4f} s")
print(f"np.pad zero: {t3:.4f} s")
print(f"np.pad wrap: {t4:.4f} s")