-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_a.py
79 lines (53 loc) · 1.99 KB
/
test_a.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 unittest
import timeit
import warnings
from prettytable import PrettyTable
import xt
import numpy as np
class test_a(unittest.TestCase):
"""
??
"""
@classmethod
def setUpClass(self):
self.a = np.random.random([103, 102, 101])
self.axis = int(np.random.randint(0, high=3))
self.data = []
@classmethod
def tearDownClass(self):
self.data = sorted(self.data, key=lambda x: x[1])
table = PrettyTable(["function", "xtensor / numpy"])
for row in self.data:
table.add_row(row)
print("")
print(table)
print("")
# code = table.get_html_string()
# with open('profile.html', 'w') as file:
# file.write(code)
def test_mean(self):
n = timeit.timeit(lambda: np.mean(self.a), number=10)
x = timeit.timeit(lambda: xt.mean(self.a), number=10)
self.data.append(("mean", x / n))
def test_flip(self):
n = timeit.timeit(lambda: np.flip(self.a, self.axis), number=10)
x = timeit.timeit(lambda: xt.flip(self.a, self.axis), number=10)
self.data.append(("flip", x / n))
def test_cos(self):
n = timeit.timeit(lambda: np.cos(self.a), number=10)
x = timeit.timeit(lambda: xt.cos(self.a), number=10)
self.data.append(("cos", x / n))
def test_isin(self):
a = (np.random.random([103, 102]) * 1000).astype(np.intc)
b = (np.random.random([103, 102]) * 1000).astype(np.intc)
n = timeit.timeit(lambda: np.isin(a, b), number=10)
x = timeit.timeit(lambda: xt.isin(a, b), number=10)
self.data.append(("isin", x / n))
def test_in1d(self):
a = (np.random.random([1003]) * 1000).astype(np.intc)
b = (np.random.random([1003]) * 1000).astype(np.intc)
n = timeit.timeit(lambda: np.in1d(a, b), number=10)
x = timeit.timeit(lambda: xt.in1d(a, b), number=10)
self.data.append(("in1d", x / n))
if __name__ == "__main__":
unittest.main()