forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_join.py
197 lines (153 loc) · 5.78 KB
/
bench_join.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import pandas.lib as lib
from pandas import *
from copy import deepcopy
import time
n = 1000000
K = 1
pct_overlap = 0.2
a = np.arange(n, dtype=np.int64)
b = np.arange(n * pct_overlap, n*(1+pct_overlap), dtype=np.int64)
dr1 = DateRange('1/1/2000', periods=n, offset=datetools.Minute())
dr2 = DateRange(dr1[int(pct_overlap*n)], periods=n, offset=datetools.Minute(2))
aobj = a.astype(object)
bobj = b.astype(object)
av = np.random.randn(n)
bv = np.random.randn(n)
avf = np.random.randn(n, K)
bvf = np.random.randn(n, K)
a_series = Series(av, index=a)
b_series = Series(bv, index=b)
a_frame = DataFrame(avf, index=a, columns=range(K))
b_frame = DataFrame(bvf, index=b, columns=range(K, 2 * K))
def do_left_join(a, b, av, bv):
out = np.empty((len(a), 2))
lib.left_join_1d(a, b, av, bv, out)
return out
def do_outer_join(a, b, av, bv):
result_index, aindexer, bindexer = lib.outer_join_indexer(a, b)
result = np.empty((2, len(result_index)))
lib.take_1d(av, aindexer, result[0])
lib.take_1d(bv, bindexer, result[1])
return result_index, result
def do_inner_join(a, b, av, bv):
result_index, aindexer, bindexer = lib.inner_join_indexer(a, b)
result = np.empty((2, len(result_index)))
lib.take_1d(av, aindexer, result[0])
lib.take_1d(bv, bindexer, result[1])
return result_index, result
from line_profiler import LineProfiler
prof = LineProfiler()
from pandas.util.testing import set_trace
def do_left_join_python(a, b, av, bv):
indexer, mask = lib.ordered_left_join_int64(a, b)
n, ak = av.shape
_, bk = bv.shape
result_width = ak + bk
result = np.empty((result_width, n), dtype=np.float64)
result[:ak] = av.T
bchunk = result[ak:]
_take_multi(bv.T, indexer, bchunk)
np.putmask(bchunk, np.tile(mask, bk), np.nan)
return result
def _take_multi(data, indexer, out):
if not data.flags.c_contiguous:
data = data.copy()
for i in xrange(data.shape[0]):
data[i].take(indexer, out=out[i])
def do_left_join_multi(a, b, av, bv):
n, ak = av.shape
_, bk = bv.shape
result = np.empty((n, ak + bk), dtype=np.float64)
lib.left_join_2d(a, b, av, bv, result)
return result
def do_outer_join_multi(a, b, av, bv):
n, ak = av.shape
_, bk = bv.shape
result_index, rindexer, lindexer = lib.outer_join_indexer(a, b)
result = np.empty((len(result_index), ak + bk), dtype=np.float64)
lib.take_join_contiguous(av, bv, lindexer, rindexer, result)
# result = np.empty((ak + bk, len(result_index)), dtype=np.float64)
# lib.take_axis0(av, rindexer, out=result[:ak].T)
# lib.take_axis0(bv, lindexer, out=result[ak:].T)
return result_index, result
def do_inner_join_multi(a, b, av, bv):
n, ak = av.shape
_, bk = bv.shape
result_index, rindexer, lindexer = lib.inner_join_indexer(a, b)
result = np.empty((len(result_index), ak + bk), dtype=np.float64)
lib.take_join_contiguous(av, bv, lindexer, rindexer, result)
# result = np.empty((ak + bk, len(result_index)), dtype=np.float64)
# lib.take_axis0(av, rindexer, out=result[:ak].T)
# lib.take_axis0(bv, lindexer, out=result[ak:].T)
return result_index, result
def do_left_join_multi_v2(a, b, av, bv):
indexer, mask = lib.ordered_left_join_int64(a, b)
bv_taken = bv.take(indexer, axis=0)
np.putmask(bv_taken, mask.repeat(bv.shape[1]), np.nan)
return np.concatenate((av, bv_taken), axis=1)
def do_left_join_series(a, b):
return b.reindex(a.index)
def do_left_join_frame(a, b):
a.index._indexMap = None
b.index._indexMap = None
return a.join(b, how='left')
# a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
# b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
# print lib.inner_join_indexer(a, b)
out = np.empty((10, 120000))
def join(a, b, av, bv, how="left"):
func_dict = {'left' : do_left_join_multi,
'outer' : do_outer_join_multi,
'inner' : do_inner_join_multi}
f = func_dict[how]
return f(a, b, av, bv)
def bench_python(n=100000, pct_overlap=0.20, K=1):
import gc
ns = [2, 3, 4, 5, 6]
iterations = 200
pct_overlap = 0.2
kinds = ['outer', 'left', 'inner']
all_results = {}
for logn in ns:
n = 10**logn
a = np.arange(n, dtype=np.int64)
b = np.arange(n * pct_overlap, n * pct_overlap + n, dtype=np.int64)
avf = np.random.randn(n, K)
bvf = np.random.randn(n, K)
a_frame = DataFrame(avf, index=a, columns=range(K))
b_frame = DataFrame(bvf, index=b, columns=range(K, 2 * K))
all_results[logn] = result = {}
for kind in kinds:
gc.disable()
elapsed = 0
_s = time.clock()
for i in range(iterations):
if i % 10 == 0:
elapsed += time.clock() - _s
gc.collect()
_s = time.clock()
a_frame.join(b_frame, how=kind)
# join(a, b, avf, bvf, how=kind)
elapsed += time.clock() - _s
gc.enable()
result[kind] = (elapsed / iterations) * 1000
return DataFrame(all_results, index=kinds)
def bench_xts(n=100000, pct_overlap=0.20):
from pandas.rpy.common import r
r('a <- 5')
xrng = '1:%d' % n
start = n * pct_overlap + 1
end = n + start - 1
yrng = '%d:%d' % (start, end)
r('library(xts)')
iterations = 500
kinds = ['left', 'outer', 'inner']
result = {}
for kind in kinds:
r('x <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, xrng))
r('y <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, yrng))
stmt = 'for (i in 1:%d) merge(x, y, join="%s")' % (iterations, kind)
elapsed = r('as.list(system.time(%s, gcFirst=F))$elapsed' % stmt)[0]
result[kind] = (elapsed / iterations) * 1000
return Series(result)