-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: is_bool_indexer #41861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PERF: is_bool_indexer #41861
Conversation
|
||
for item in obj: | ||
if not util.is_bool_object(item): | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any concern with the benchmarks being somewhat misleading due to short circuiting? At least in the Python space similar functions don't seem to benefit much from this
In [6]: %timeit all(isinstance(x, bool) for x in obj)
6.7 µs ± 136 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [7]: %timeit np.array(obj).dtype.kind == "b"
5.98 µs ± 121 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any concern with the benchmarks being somewhat misleading due to short circuiting?
Only one of the 4 cases posted short-circuits. Am I misunderstanding the question?
At least in the Python space similar functions don't seem to benefit much from this
I was surprised by this too, found that doing it in cython was much more performant after talking with @seberg yesterday about how numpy does this inference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, the difference is big...
NumPy should maybe try to be a bit faster, but that will just make the gap a bit smaller (e.g. by assuming that usually all elements are the same type, I guess a true casting support np.asarray(..., dtype=bool, casting="equiv")
would fill the gap, but no idea how likely that would be). Unlike NumPy, you already know what you want here (bools) and what you got (a 1-d list).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah my bad on short circuiting - misread it. Thanks for comments
thanks this looks good |
Besides avoiding an array allocation, thus avoids the np.asarray call that was one of the main offenders in #41632