如果我误用了任何术语,请提前更正。
我有一个排序数组dtype
'<f16, |S30'
。当我在第一个字段上使用searchsorted
时,它的工作非常慢(对于300万个项目,大约0.4秒)。这比bisect
在普通Python元组列表上执行相同操作所需的时间要长得多。在
%timeit a['f0'].searchsorted(400.)
1 loops, best of 3: 398 ms per loop
但是,如果我将float部分复制到另一个单独的数组中,搜索速度比bisect
快:
我的问题是:
下面是一些代码来说明问题的大小(截至2015年5月11日)以及如何“修复”它。在
更新! 所以多亏了Rico的评论,似乎为你想要搜索的数字设置类型sorted/bisect真的很重要! 但是,在具有32位和16位整数的结构化数组上,它仍然很慢(尽管没有任何地方比以前慢)
我记得不久前见过这个。如果我没记错的话,我认为searchsorted会在数据不连续的情况下生成数据的临时副本。如果以后有时间,我会查看代码以确认发生了什么(或者也许更熟悉代码的人可以确认这一点)。在
同时,如果您不想重新构造代码以避免使用结构化数组,那么最好的选择可能是使用
bisect_left(a['f0'], 400.)
。在我的机器上,它比连续数组上的searchsorted慢8倍,但比非连续数组上的searchsorted快1000倍。在相关问题 更多 >
编程相关推荐