-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Consider conditionally applying rule PLR6201 #8758
Comments
There are optimizations here that are not accounted for in your benchmarks. See What’s New In Python 3.2 for example. Also see #8322 and pylint-dev/pylint#4776. |
Yup, with these, the $ python -m timeit '0 in {0, "0", 9, "9", 8, "8", 7, "7", 6, "6"}'
20000000 loops, best of 5: 13.6 nsec per loop
$ python -m timeit '0 in [0, "0", 9, "9", 8, "8", 7, "7", 6, "6"]'
20000000 loops, best of 5: 11.5 nsec per loop
$ python -m timeit '0 in (0, "0", 9, "9", 8, "8", 7, "7", 6, "6")'
20000000 loops, best of 5: 11.4 nsec per loop
$ python -m timeit '6 in {0, "0", 9, "9", 8, "8", 7, "7", 6, "6"}'
20000000 loops, best of 5: 12.9 nsec per loop
$ python -m timeit '6 in [0, "0", 9, "9", 8, "8", 7, "7", 6, "6"]'
5000000 loops, best of 5: 52.3 nsec per loop
$ python -m timeit '6 in (0, "0", 9, "9", 8, "8", 7, "7", 6, "6")'
5000000 loops, best of 5: 56.5 nsec per loop The manual version is maybe even a tiny bit faster, but this is noise level. $ python -m timeit -s 'seq = {0, "0", 9, "9", 8, "8", 7, "7", 6, "6"}' '0 in seq'
20000000 loops, best of 5: 12.5 nsec per loop
$ python -m timeit -s 'seq = [0, "0", 9, "9", 8, "8", 7, "7", 6, "6"]' '0 in seq'
20000000 loops, best of 5: 11.2 nsec per loop
$ python -m timeit -s 'seq = (0, "0", 9, "9", 8, "8", 7, "7", 6, "6")' '0 in seq'
20000000 loops, best of 5: 10.5 nsec per loop
$ python -m timeit -s 'seq = {0, "0", 9, "9", 8, "8", 7, "7", 6, "6"}' '6 in seq'
20000000 loops, best of 5: 15.3 nsec per loop
$ python -m timeit -s 'seq = [0, "0", 9, "9", 8, "8", 7, "7", 6, "6"]' '6 in seq'
5000000 loops, best of 5: 53.8 nsec per loop
$ python -m timeit -s 'seq = (0, "0", 9, "9", 8, "8", 7, "7", 6, "6")' '6 in seq'
5000000 loops, best of 5: 49.6 nsec per loop |
@flying-sheep Thanks! Could you also please run an equivalent benchmark for a 3-item collection? 🙏 |
Microoptimizations like this are rarely indicative of real life performance. I’m simply pointing out that the optimization mentioned by @tdulcet is indeed applied, so using a inline set literal is preferable, as it’s equivalent to creating a frozenset ahead of time. But sure, here you go. Finding the 3rd item in a sequence is slower than finding it in a set, only finding the first item in a sequence is very very slightly faster than finding that item in a set. So Peephole optimized: $ python -m timeit '0 in {0, "0", 9}'
20000000 loops, best of 5: 13.7 nsec per loop
$ python -m timeit '0 in [0, "0", 9]'
20000000 loops, best of 5: 11.4 nsec per loop
$ python -m timeit '0 in (0, "0", 9)'
20000000 loops, best of 5: 11.3 nsec per loop
$ python -m timeit '9 in {0, "0", 9}'
20000000 loops, best of 5: 12.8 nsec per loop
$ python -m timeit '9 in [0, "0", 9]'
10000000 loops, best of 5: 21.4 nsec per loop
$ python -m timeit '9 in (0, "0", 9)'
10000000 loops, best of 5: 21.5 nsec per loop … has the same performance as manually optimized: $ python -m timeit -s 'seq = {0, "0", 9}' '0 in seq'
20000000 loops, best of 5: 12.7 nsec per loop
$ python -m timeit -s 'seq = [0, "0", 9]' '0 in seq'
20000000 loops, best of 5: 11.3 nsec per loop
$ python -m timeit -s 'seq = (0, "0", 9)' '0 in seq'
20000000 loops, best of 5: 10.5 nsec per loop
$ python -m timeit -s 'seq = {0, "0", 9}' '9 in seq'
20000000 loops, best of 5: 12.2 nsec per loop
$ python -m timeit -s 'seq = [0, "0", 9]' '9 in seq'
10000000 loops, best of 5: 21.7 nsec per loop
$ python -m timeit -s 'seq = (0, "0", 9)' '9 in seq'
20000000 loops, best of 5: 20 nsec per loop |
Thanks for the details @flying-sheep |
Would be nice to add link to this ticket to docs as many people trying to benchmark it:
|
When the sequences are small, the cost of instantiation of a set in fact negatively impacts performance:
The text was updated successfully, but these errors were encountered: