-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
new rule: remove unnecessary list wrapping #2502
Comments
This was discussed way back when for It was rejected because using a list comprehension was faster, and still is: # Python 3.10.9
In [1]: %timeit ",".join([str(i) for i in range(3)])
389 ns ± 1.79 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [2]: %timeit ",".join([str(i) for i in range(300)])
17.9 µs ± 90.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [3]: %timeit ",".join(str(i) for i in range(3))
473 ns ± 1.68 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [4]: %timeit ",".join(str(i) for i in range(300))
20.8 µs ± 21.4 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
# Python 3.11.1
In [1]: %timeit ",".join([str(i) for i in range(3)])
384 ns ± 4.26 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [2]: %timeit ",".join([str(i) for i in range(300)])
16.2 µs ± 169 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [3]: %timeit ",".join(str(i) for i in range(3))
501 ns ± 5.38 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [4]: %timeit ",".join(str(i) for i in range(300))
19.9 µs ± 268 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) |
So strange that that's faster! Intuitively I would've expected the opposite. |
ah, I saw this in the past as well. The reason:
But I think the performance aspect doesn't matter here, it's minimal in the recent python versions. So a new rule which can do this and be deactivated by folks who want the faster version. |
I might err on the side of omitting if |
(Even though I personally wouldn't mind it.) |
Out of interest, the performance dip for 3.11 in the third test (small generator) is likely caused by python/cpython#100762. Even then, it'll still be slower. If we did implement this, it should be a different rule to |
I'm going to close for now, we can re-open if |
''.join([chr(i) for i in range(128)])
should be:
''.join(chr(i) for i in range(128))
The text was updated successfully, but these errors were encountered: