Skip to content

Instantly share code, notes, and snippets.

@MrBago
Last active December 17, 2015 22:29
Show Gist options
  • Save MrBago/7d832248499596356039 to your computer and use it in GitHub Desktop.
Save MrBago/7d832248499596356039 to your computer and use it in GitHub Desktop.
A few benchmarks to time `numpy.searchsorted` implementations.
#!/usr/bin/env python
import numpy as np
from timeit import timeit
setup = "from __main__ import array, keys"
N = 10000
np.random.seed(0)
keys = np.random.random(N)
# Search a contiguous array for a single key
array = np.linspace(0, 1., 1*N)
time = timeit("array.searchsorted(.6)", setup, number=100000)
print "contiguous, one key: ", time
# Search a non-contiguous array for a single key
array = np.linspace(0, 1., 2*N)[::2]
time = timeit("array.searchsorted(.6)", setup, number=100000)
print "non-contiguous, one key: ", time
# And again with very large stride
array = np.linspace(0, 1., 100*N)[::100]
time = timeit("array.searchsorted(.6)", setup, number=100000)
print "large stride, one key: ", time
# Search a contiguous array for many keys
array = np.linspace(0, 1., 1*N)
time = timeit("array.searchsorted(keys)", setup, number=1000)
print "contiguous, many keys: ", time
# Search a non-contiguous array for a single key
array = np.linspace(0, 1., 2*N)[::2]
time = timeit("array.searchsorted(keys)", setup, number=1000)
print "non-contiguous, many keys: ", time
# And again with very large stride
array = np.linspace(0, 1., 100*N)[::100]
time = timeit("array.searchsorted(keys)", setup, number=1000)
print "large stride, many keys: ", time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment