【发布时间】:2014-01-13 05:21:20
【问题描述】:
以下代码创建一个测试hdf5文件:
from tables import *
class KeyValue(IsDescription):
key = StringCol(itemsize=30, dflt=" ", pos=0) # character String
value = Int64Col(dflt=0, pos=1)
f = open_file("keyvalue.h5", "w")
kv = f.create_table("/", "keyvalues", KeyValue)
import string
import random
for j in range(20):
values = []
for i in xrange(100000):
key = "".join(random.sample(string.uppercase, 5))
value = random.randint(0, 1000000)
values.append((key, value))
kv.append(values)
f.close()
这里是速度测试代码:
f = open_file("keyvalue.h5", "a")
kv = f.root.keyvalues
kv.cols.value.remove_index()
print "without index"
%timeit a = kv.read_where('value < 10')
%timeit a = kv.read_where('value < 1000')
kv.cols.value.create_csindex()
print "with index"
%timeit a = kv.read_where('value < 10')
%timeit a = kv.read_where('value < 1000')
f.close()
输出是:
without index
10 loops, best of 3: 66.1 ms per loop
10 loops, best of 3: 109 ms per loop
with index
10000 loops, best of 3: 164 µs per loop
10 loops, best of 3: 121 ms per loop
查询'value
我想知道这是怎么发生的,您能否将timeit 结果发布到您的
机器?
【问题讨论】:
-
请注意,您的代码示例似乎在 ipython 中,而不是纯 python。
标签: python indexing hdf5 pytables