Created
February 15, 2021 15:31
-
-
Save StefRe/1d73c663d5b162cbda7bb3dd6d838a19 to your computer and use it in GitHub Desktop.
Performance comparison for https://stackoverflow.com/a/66102212/3944322
This file contains hidden or 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
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}') |
Author
StefRe
commented
Feb 15, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment