[Numpy-discussion] Fancy Indexing of Structured Arrays is Slow

Dave Hirschfeld dave.hirschfeld at gmail.com
Thu May 15 08:31:50 EDT 2014

As can be seen from the code below (or in the notebook linked beneath) fancy 
indexing of a structured array is twice as slow as indexing both fields 
independently - making it 4x slower?

I found that fancy indexing was a bottleneck in my application so I was 
hoping to reduce the overhead by combining the arrays into a structured 
array and only doing one indexing operation. Unfortunately that doubled the 
time that it took!

Is there any reason for this? If not, I'm happy to open an enhancement issue 
on GitHub - just let me know.


In [32]: nrows, ncols = 365, 10000

In [33]: items = np.rec.fromarrays(randn(2,nrows, ncols), names=

In [34]: row_idx = randint(0, nrows, ncols)
    ...: col_idx = np.arange(ncols)

In [35]: %timeit filtered_items = items[row_idx, col_idx]
100 loops, best of 3: 3.45 ms per loop

In [36]: %%timeit 
    ...: widgets = items['widgets'][row_idx, col_idx]
    ...: gadgets = items['gadgets'][row_idx, col_idx]
1000 loops, best of 3: 1.57 ms per loop



More information about the NumPy-Discussion mailing list