-
-
Save MrBago/7d832248499596356039 to your computer and use it in GitHub Desktop.
A few benchmarks to time `numpy.searchsorted` implementations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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