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

bpo-38045: Improve the performance of _decompose() in the enum.py #16483

Merged
merged 2 commits into from
Nov 26, 2019

Conversation

hongweipeng
Copy link
Contributor

@hongweipeng hongweipeng commented Sep 30, 2019

The original code will access each element of _value2member_map_ and check if the value is the power of 2.

After optimization, access the elements in _value2member_map_ as needed. In the initial example:

from enum import Flag
F = Flag("F", list("abcdefghijklm"))
for idx in range(2**len(F) - 1):
    F(idx)

the speedup is more than 100x .(The same for replacing Flag with IntFlag).

https://bugs.python.org/issue38045

@corona10
Copy link
Member

@hongweipeng Can you show me the benchmark result?

@hongweipeng
Copy link
Contributor Author

Use the initial example:

from enum import Flag
F = Flag("F", list("abcdefghijklm"))
for idx in range(2**len(F) - 1):
    F(idx)

Before patch:

# time ./python ts.py

real	0m29.639s
user	0m29.615s
sys	0m0.020s

After patch:

# time ./python ts.py

real	0m0.135s
user	0m0.122s
sys	0m0.014s

@corona10
Copy link
Member

@ethanfurman

For core-developers,
I didn't check this PR is the correct way.
But seems like there is performance enhancement with this PR.

I left the benchmark for the core-developers

Baseline

 ./python.exe -m pyperf timeit -s 'from enum import Flag' 'F = Flag("F", list("abcdefghijklm"))' 'for idx in range(100):' '   F(idx)'
.....................
Mean +- std dev: 24.5 ms +- 0.3 ms

PR

./python.exe -m pyperf timeit -s 'from enum import Flag' 'F = Flag("F", list("abcdefghijklm"))' 'for idx in range(100):' '   F(idx)'
.....................
Mean +- std dev: 7.30 ms +- 0.19 ms

@brandtbucher brandtbucher added the performance Performance or resource usage label Oct 1, 2019
Co-Authored-By: Brandt Bucher <[email protected]>
@ethanfurman ethanfurman self-assigned this Nov 16, 2019
@ethanfurman
Copy link
Member

GitHub seem stuck on bedevere/news -- closing and reopening...

@ethanfurman ethanfurman reopened this Nov 26, 2019
@ethanfurman ethanfurman merged commit 0b41a92 into python:master Nov 26, 2019
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
…GH-16483)

* Improve the performance of _decompose() in enum.py

Co-Authored-By: Brandt Bucher <[email protected]>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
…GH-16483)

* Improve the performance of _decompose() in enum.py

Co-Authored-By: Brandt Bucher <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants