-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_exponent.py
72 lines (50 loc) · 1.65 KB
/
array_exponent.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
import numexpr as ne
import numpy as np
import time
print("Configuration")
# Python float is 64 bit / 8 byte
ARRAY_SIZE = 1000 * 1000 * 10
print("- Size of data array: {} MB".format(ARRAY_SIZE * 8 / (1000 * 1000)))
N = 100
print("- Number of iterations to average: {}".format(N))
def SETUP():
array = np.ones(ARRAY_SIZE)
return array
if __name__=="__main__":
print("{:45}{:<15}{:<15}".format("Case description", "Total time", "Per-iteration time"))
array = SETUP()
elapsed_time = 0.0
for _ in range(N):
start = time.time()
_ = array * array * array * array
end = time.time()
elapsed_time += end - start
average = elapsed_time / N
print("{:45}{:<15.6f}{:<15.6f}".format("Expanded", elapsed_time, average))
array = SETUP()
elapsed_time = 0.0
for _ in range(N):
start = time.time()
_ = np.power(array, 4)
end = time.time()
elapsed_time += end - start
average = elapsed_time / N
print("{:45}{:<15.6f}{:<15.6f}".format("np.power", elapsed_time, average))
array = SETUP()
elapsed_time = 0.0
for _ in range(N):
start = time.time()
_ = array ** 4
end = time.time()
elapsed_time += end - start
average = elapsed_time / N
print("{:45}{:<15.6f}{:<15.6f}".format("Intrinsic (**)", elapsed_time, average))
array = SETUP()
elapsed_time = 0.0
for _ in range(N):
start = time.time()
_ = ne.evaluate("array ** 4")
end = time.time()
elapsed_time += end - start
average = elapsed_time / N
print("{:45}{:<15.6f}{:<15.6f}".format("numexpr", elapsed_time, average))