sorting numpy structured and record arrays is very slow

后端 未结 2 1780
忘了有多久
忘了有多久 2020-12-11 17:22

it looks like sorting numpy structured and record arrays by a single column is much slower than doing a sort on a similar standalone array:

In [111]: a = np         


        
相关标签:
2条回答
  • 2020-12-11 17:38

    As Jaime have said, you can use argsort to sort the record array.

    inds = np.argsort(rec['f0'])
    

    And use take to avoid making a copy

    np.take(rec, inds, out=rec)
    
    0 讨论(0)
  • 2020-12-11 17:48

    What´s slowing you is the use of order, not the fact that you have a record array. If you want to sort by a single field, do it like this:

    In [12]: %timeit np.argsort(rec['f0'])
    1000 loops, best of 3: 829 us per loop
    

    Once order is used, performance goes south no matter how many fields you want to sort by:

    In [16]: %timeit np.argsort(rec, order=['f0'])
    10 loops, best of 3: 27.9 ms per loop
    
    In [17]: %timeit np.argsort(rec, order=['f0', 'f1'])
    10 loops, best of 3: 28.4 ms per loop
    
    0 讨论(0)
提交回复
热议问题