-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_speed_test.py
52 lines (39 loc) · 1.35 KB
/
random_speed_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
import timeit
import numpy as np
def create_setup(size):
return """
from random import randrange, choices
import numpy as np
SIZE = {}
weights = np.random.rand(SIZE)
s = np.sum(weights)
weights /= s
arr = list(range(SIZE))
""".format(size)
random_style = """
cum_weights = np.cumsum(weights)
choices(arr, cum_weights=cum_weights, k=50)
"""
numpy_style = """
np.random.choice(SIZE, 50, p=weights)
"""
def timer(s, v='', nloop=10000, nrep=3):
units = ["s", "ms", "µs", "ns"]
scaling = [1, 1e3, 1e6, 1e9]
Timer = timeit.Timer(stmt=s, setup=v)
best = min(Timer.repeat(nrep, nloop)) / nloop
if best > 0.0:
order = min(-int(np.floor(np.log10(best)) // 3), 3)
else:
order = 3
print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
3,
best * scaling[order],
units[order]))
for k in [100, 1000, 10000, 100000, 500000]:
print("Random", k)
timer(random_style, create_setup(k))
print("Numpy", k)
timer(numpy_style, create_setup(k))
# random.choices is significantly faster when both functions are used in optimal conditions
# random.choices is still faster than numpy.choices if we perform the cumsum with numpy right before the function call