Skip to content

Instantly share code, notes, and snippets.

@mverleg
Created April 25, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mverleg/3b63d70c806ea4eb73168870297d0bcb to your computer and use it in GitHub Desktop.
Save mverleg/3b63d70c806ea4eb73168870297d0bcb to your computer and use it in GitHub Desktop.
"""
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()
@mverleg
Copy link
Author

mverleg commented Apr 25, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment