-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.py
61 lines (49 loc) · 1.33 KB
/
bench.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
import numpy as np
import pandas as pd
import rollingrank
from pythonbenchmark import measure
x = np.random.rand(1024 * 1024)
x_nan = x.copy()
x_nan[np.arange(0, x.size, 2)] = np.nan
x_small = np.random.rand(16 * 1024)
window = 1024
window_small = 16
@measure
def bench():
rollingrank.rollingrank(x, window=window)
@measure
def bench_single():
rollingrank.rollingrank(x, window=window, n_jobs=1)
@measure
def bench_float():
rollingrank.rollingrank(x.astype('float32'), window=window)
@measure
def bench_nan():
rollingrank.rollingrank(x_nan, window=window)
@measure
def bench_pct():
rollingrank.rollingrank(x, window=window, pct=True)
def rollingrank_pandas(x, window=None):
def to_rank(x):
# result[i] is the rank of x[i] in x
x = x.values
return np.sum(x < x[-1])
return pd.Series(x).rolling(window).apply(to_rank).values
def rollingrank_pandas2(x, window=None):
def to_rank(x):
# result[i] is the rank of x[i] in x
return x.values.argsort().argsort()[-1]
return pd.Series(x).rolling(window).apply(to_rank).values
@measure
def bench_pandas():
rollingrank_pandas(x_small, window=window_small)
@measure
def bench_pandas2():
rollingrank_pandas2(x_small, window=window_small)
bench()
bench_single()
bench_float()
bench_nan()
bench_pct()
bench_pandas()
bench_pandas2()