Skip to content

Instantly share code, notes, and snippets.

@StefRe
Created February 15, 2021 15:31
Show Gist options
  • Save StefRe/1d73c663d5b162cbda7bb3dd6d838a19 to your computer and use it in GitHub Desktop.
Save StefRe/1d73c663d5b162cbda7bb3dd6d838a19 to your computer and use it in GitHub Desktop.
import numpy as np
import perfplot
import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
xy = [51,52]
maxint = 10
def setup(n):
np.random.seed(0)
a = np.random.randint(0,maxint,n)
a[:2] = xy
return a
def unique_without_index(vec):
first_occurrence = []
for x in np.unique(vec):
if x not in xy:
first_occurrence.append(np.argmax(x==vec))
return first_occurrence
def unique_with_index(vec):
u, i = np.unique(vec, return_index=True)
return i[np.isin(u, xy, invert=True)]
def isin_sorted(a, i, invert=False):
ind = np.searchsorted(a, i)
ind = ind[a[ind.clip(max=a.size)] == i]
if invert:
mask = np.ones(a.size, dtype=bool)
mask[ind] = False
else:
mask = np.zeros(a.size, dtype=bool)
mask[ind] = True
return mask
def unique_with_index_sorted(vec):
u, i = np.unique(vec, return_index=True)
return i[isin_sorted(u, xy, invert=True)]
perfplot.show(
setup=setup,
kernels=[
lambda a: unique_without_index(a),
lambda a: unique_with_index(a),
lambda a: unique_with_index_sorted(a),
],
labels=["unique_without_index", "unique_with_index", "unique_with_index_sorted"],
n_range=[2 ** k for k in range(5,25)],
xlabel="len(vec) xy = [51,52] ",
)
ax = plt.gca()
ax.spines['bottom'].set_visible(True)
ax.set_ylabel(ax.get_title())
ax.set_title(f'numbers 0 .. {maxint}')
@StefRe
Copy link
Author

StefRe commented Feb 15, 2021

vec = np.array([2, 2, 2, 51, 51, 52, 52, 14, 14, 14, 51, 51, 52, 52])
xy = [51,52]

%timeit unique_with_index(vec)
#27.5 µs ± 185 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit unique_with_index_sorted(vec)
#38.9 µs ± 109 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit unique_without_index(vec)
#16.2 µs ± 43.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

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