-
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
RUF017 Avoid quadratic list summation: consider itertools.chain.from_iterable #9045
Comments
This makes sense... We could at least do this for cases in which it's directly within a |
Interestingly, though, the import timeit
code1 = """
tags = sorted(itertools.chain.from_iterable(mess))
"""
code2 = """
tags = sorted(functools.reduce(operator.iadd, [x for x in mess], []))
"""
repeats = 10000
time1 = timeit.timeit(stmt=code1, setup='import itertools\nmess = [[i, i + 1] for i in range(10000)]', number=repeats)
print(f"{time1} seconds")
time2 = timeit.timeit(stmt=code2, setup='import itertools\nimport functools\nimport operator\nmess = [[i] for i in range(10000)]', number=repeats)
print(f"{time2} seconds") Yields:
|
I suspect this relates to different optimization levels of the Code paths |
Here's some more benchmarking from when we added this: #5073 (comment) |
I'd recommend closing this issue, since the functools variant is meaningfully faster: #5073 (comment) |
I agree. |
currently
RUF017
takes
and turns it into
instead of the more idiomatic
based on the applications of the pattern i have seen so far, i would almost always recommend to replace
the sum with a
list(chain.from_iterable(...))
with the additional "optimization" of in-lining list comprehensions and/or generator expressions when applicable
The text was updated successfully, but these errors were encountered: