forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_khash_dict.py
78 lines (58 loc) · 1.84 KB
/
bench_khash_dict.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
"""
Some comparisons of khash.h to Python dict
"""
import numpy as np
import os
from vbench.api import Benchmark
from pandas.util.testing import rands
import pandas._tseries as lib
import pandas._sandbox as sbx
import time
import psutil
pid = os.getpid()
proc = psutil.Process(pid)
def object_test_data(n):
pass
def string_test_data(n):
return np.array([rands(10) for _ in xrange(n)], dtype='O')
def int_test_data(n):
return np.arange(n, dtype='i8')
N = 1000000
#----------------------------------------------------------------------
# Benchmark 1: map_locations
def map_locations_python_object():
arr = string_test_data(N)
return _timeit(lambda: lib.map_indices_object(arr))
def map_locations_khash_object():
arr = string_test_data(N)
def f():
table = sbx.PyObjectHashTable(len(arr))
table.map_locations(arr)
return _timeit(f)
def _timeit(f, iterations=10):
start = time.time()
for _ in xrange(iterations):
foo = f()
elapsed = time.time() - start
return elapsed
#----------------------------------------------------------------------
# Benchmark 2: lookup_locations
def lookup_python(values):
table = lib.map_indices_object(values)
return _timeit(lambda: lib.merge_indexer_object(values, table))
def lookup_khash(values):
table = sbx.PyObjectHashTable(len(values))
table.map_locations(values)
locs = table.lookup_locations(values)
# elapsed = _timeit(lambda: table.lookup_locations2(values))
return table
def leak(values):
for _ in xrange(100):
print proc.get_memory_info()
table = lookup_khash(values)
# table.destroy()
arr = string_test_data(N)
#----------------------------------------------------------------------
# Benchmark 3: unique
#----------------------------------------------------------------------
# Benchmark 4: factorize