Created
April 25, 2016 12:29
-
-
Save mverleg/3b63d70c806ea4eb73168870297d0bcb to your computer and use it in GitHub Desktop.
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
""" | |
http://stackoverflow.com/questions/7632963/numpy-find-first-index-of-value-fast | |
First you need to run: | |
f2py -c -m search search.f90 | |
""" | |
from time import time | |
from matplotlib.pyplot import subplots, show | |
from numpy import array, nonzero, argwhere | |
import search | |
print(search.find_first.__doc__) | |
D = array(range(1000000)) | |
def find_first(vec, needle): | |
for k, val in enumerate(vec): | |
if val == needle: | |
return k | |
return -1 | |
needles = [1, 10, 100, 1000, 10000, 100000, 1000000-1, 2000000] | |
out = [] | |
a = search.find_first(10, D) | |
reps = 100 | |
for needle in needles: | |
print('benchmark for needle at {0:d}'.format(needle)) | |
res = [] | |
t = time() | |
for k in range(reps): | |
try: | |
a = argwhere(D==needle)[0][0] | |
except IndexError: | |
a = -1 | |
res.append(time() - t) | |
t = time() | |
for k in range(reps): | |
try: | |
a = nonzero(D == needle)[0][0] | |
except IndexError: | |
a = -1 | |
res.append(time() - t) | |
t = time() | |
for k in range(reps): | |
a = (D == needle).tostring().find('\x01') | |
res.append(time() - t) | |
t = time() | |
for k in range(reps): | |
a = find_first(D, needle) | |
res.append(time() - t) | |
t = time() | |
for k in range(reps): | |
a = search.find_first(needle, D) | |
res.append(time() - t) | |
out.append(res) | |
outa = array(out) / reps | |
names = ['argwhere', 'nonzero', 'tostring', 'pyloop', 'f2py'] | |
fig, ax = subplots(figsize=(5, 4)) | |
fig.tight_layout() | |
for k, col in enumerate(outa.T): | |
ax.loglog(needles, col, label=names[k]) | |
ax.legend(loc='upper left') | |
if __name__ == '__main__': | |
show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fortran code: https://gist.github.com/mverleg/71dbbf755ed1cf96589236a69bc64027