-
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
New Rule: Prefer list comprehension over generator comprehensions to create tuples #11839
Comments
Using your script, I see less of a difference on Linux with CPython:
But much more of a difference with PyPy:
|
I think tuple-from-list comprehension approach will lead to to 2x higher peak memory consumption, won't it? |
It may be faster, but I think it's less legible since it introduces a useless list comprehension. I think it would be better to post this on discuss to see the if core developers can make comprehension creation faster. |
Also, I came here to post a related issue, but probably the discussion belongs here since it's close enough: In [1]: %timeit x = [1, *[2 for _ in range(10)]]
156 ns ± 3.32 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
In [2]: %timeit x = [1, *(2 for _ in range(10))]
337 ns ± 9.72 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) Ruff doesn't seem to have an opinion on whether an unpacked nested iterable should be written as a list or generator. I think Ruff should have an opinion, and from a legibility standpoint, I prefer generators even though they are slower. |
I was recently working on some bits of codes where most of my data had to be "readonly" (so I'm using immutable types like frozen dataclasses, frozensets, tuples, etc.) but also using plenty of comprehensions. Which made me wonder, since there's no "tuple comprehension" in Python, how I should be writing this code. I did a bit of performance testing, and here's the results:
Unsurprisingly, the difference is even greater in 3.12 with inline list comprehension.
Because of the
tuple
, the generator is immediately iterated, so you get no benefit from its "lazyness". This is probably true for other stdlib collections that don't have a comprehension syntax, tuple is just the only one I can think of atm.For this reason, I'm asking for a performance rule with an autofix that transforms code like this:
into
Which, unless I'm missing something, is free performance whilst staying readable and pythonic.
It seems this would fit well in the
flake8-comprehensions
orrefurb
family of rules.The text was updated successfully, but these errors were encountered: