Skip to content
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

ImageMath: Inline isinstance check #7623

Merged

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Dec 18, 2023

_isconstant() is short and only called once, we might as well inline it, it's also quicker:

python -m timeit -s "def _isconstant(v): return isinstance(v, (int, float))" "_isconstant(12.34)"
5000000 loops, best of 5: 54.1 nsec per looppython -m timeit "isinstance(12.34, (int, float))"
10000000 loops, best of 5: 38.3 nsec per loop

History:

The current one is from 2019:

def _isconstant(v):
    return isinstance(v, (int, float))

The 2013, 2to3 version was:

def _isconstant(v):
    return isinstance(v, int) or isinstance(v, float)

The original 2006, PIL 1.1.6 version was:

def _isconstant(v):
    return isinstance(v, type(0)) or isinstance(v, type(0.0))

@hugovk hugovk added the Cleanup label Dec 18, 2023
@radarhere radarhere merged commit 67890b2 into python-pillow:main Dec 18, 2023
55 of 56 checks passed
@hugovk hugovk deleted the cleanup-imagemath-inline-isinstance branch December 18, 2023 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants