numpy.searchsorted的表现是结构化阵列差 [英] Performance of numpy.searchsorted is poor on structured arrays

查看:546
本文介绍了numpy.searchsorted的表现是结构化阵列差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<子>对不起提前如果我滥用的任何条款,随时纠正。

我有 DTYPE 排序后的数组'&LT; F16,| S30 。当我使用 searchsorted 在其第一个字段,它的工作原理很慢(约0.4秒,300万件)。这比更长开张需要做同样的元组的普通的Python列表。

 %timeit一个['F0'。searchsorted(400)。
1循环,最好的3:每循环398毫秒

不过,如果我浮起部分复制到另一个单独的数组中,搜索是比开张更快

  B = A ['F0']。副本()%timeit b.searchsorted(400)。
百万循环,最好的3:每回路945纳秒

我的问题是:


  1. 我是不是做错了什么或者是在numpy的回归?

  2. 有没有办法绕过这个没有数据的重复?


解决方案

我记得看到这一段时间以前。如果我没有记错,我觉得searchsorted使得数据的临时副本时数据不连续的。如果我以后有时间,我会看看在code确认这是发生了什么(或者也许有人更熟悉code可以证实这一点)。

在此同时,如果您不希望您的重组code避免使用结构化阵列,最好的办法可能是使用 bisect_left(A ['F0'], 400)。在我的机器,它比searchsorted连续阵列上慢8倍,但比searchsorted非连续阵列上快1000倍。

 在[5]:A = np.arange((6E6))视图([(F0,浮),('F1',浮动)])在[6]:timeit一个['F0'] searchsorted。(400)。
10圈,最好的3:每循环51.1毫秒[7]:timeit一个['F0']副本()。
10圈,最好的3:每循环51毫秒在[8]:timeit bisect_left(一个['F0'],400)
10000循环,最好的3:52.8我们每个环路在[9]:F0 = A ['F0']副本()在[10]:timeit f0.searchsorted(400)。
100000循环,最好的3:7.85为每循环

Sorry in advance if I'm misusing any terms, feel free to correct that.

I have a sorted array with dtype '<f16, |S30'. When I use searchsorted on its first field, it works really slow (about 0.4 seconds for 3 million items). That is much longer than bisect takes to do the same on a plain Python list of tuples.

%timeit a['f0'].searchsorted(400.)
1 loops, best of 3: 398 ms per loop

However, if I copy the float part to another, separate array, the search is faster than bisect:

b = a['f0'].copy()

%timeit b.searchsorted(400.)
1000000 loops, best of 3: 945 ns per loop

My questions are:

  1. Am I doing something wrong or is it a regression in NumPy?
  2. Is there a way to circumvent this without duplication of the data?

解决方案

I remember seeing this some time ago. If I remember correctly, I think searchsorted makes a temporary copy of the data when the data is not contiguous. If I have time later, I'll take a look at the code to confirm that's what's happening (or maybe someone more familiar with the code can confirm this).

In the mean time, if you don't want to restructure your code to avoid using a structured array, your best bet is probably to use bisect_left(a['f0'], 400.). On my machine it's 8x slower than searchsorted on a contiguous array but 1000x faster than searchsorted on a non-contiguous array.

In [5]: a = np.arange((6e6)).view([('f0', float), ('f1', float)])

In [6]: timeit a['f0'].searchsorted(400.)
10 loops, best of 3: 51.1 ms per loop

In [7]: timeit a['f0'].copy()
10 loops, best of 3: 51 ms per loop

In [8]: timeit bisect_left(a['f0'], 400.)
10000 loops, best of 3: 52.8 us per loop

In [9]: f0 = a['f0'].copy()

In [10]: timeit f0.searchsorted(400.)
100000 loops, best of 3: 7.85 us per loop

这篇关于numpy.searchsorted的表现是结构化阵列差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆