Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Last active August 29, 2015 13:57
Show Gist options
  • Save cwidmer/9676560 to your computer and use it in GitHub Desktop.
Save cwidmer/9676560 to your computer and use it in GitHub Desktop.
"""
snippet to time different indexing strategies
based on:
http://wesmckinney.com/blog/?p=215
http://stackoverflow.com/questions/11800075/faster-numpy-fancy-indexing-and-reduction/11813040#11813040
http://stackoverflow.com/questions/14386822/fast-numpy-fancy-indexing?rq=1
"""
import numpy as np
import random
indexer = np.arange(5000)
random.shuffle(indexer)
arr = np.array(np.random.randn(10000, 10000), order="F")
%timeit arr[:,indexer]
%timeit arr.take(indexer, axis=1)
arr = np.array(np.random.randn(10000, 10000), order="C")
%timeit arr[:,indexer]
%timeit arr.take(indexer, axis=1)
"""
In [46]: indexer = np.arange(5000)
In [47]: random.shuffle(indexer)
In [48]:
In [48]: arr = np.array(np.random.randn(10000, 10000), order="F")
In [49]: %timeit arr[:,indexer]
1 loops, best of 3: 1.41 s per loop
In [50]: %timeit arr.take(indexer, axis=1)
1 loops, best of 3: 2.35 s per loop
In [51]:
In [51]: arr = np.array(np.random.randn(10000, 10000), order="C")
In [52]: %timeit arr[:,indexer]
1 loops, best of 3: 3.08 s per loop
In [53]: %timeit arr.take(indexer, axis=1)
1 loops, best of 3: 438 ms per loop
"""
# verdict: use C-order with take
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment