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

Minor optimizations by using comprehensions #182

Closed
wants to merge 1 commit into from

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Apr 28, 2024

% ruff check --select=C4,PERF --ignore=PERF203

pyperf/__main__.py:355:21: PERF401 Use a list comprehension to create a transformed list
pyperf/_bench.py:563:21: PERF401 Use a list comprehension to create a transformed list
pyperf/_compare.py:168:13: PERF401 Use a list comprehension to create a transformed list
pyperf/_compare.py:253:21: C414 Unnecessary `list` call within `sorted()`
pyperf/_compare.py:285:13: PERF401 Use a list comprehension to create a transformed list
pyperf/_compare.py:432:13: PERF401 Use a list comprehension to create a transformed list
pyperf/_cpu_utils.py:94:17: PERF402 Use `list` or `list.copy` to create a copy of a list
pyperf/_utils.py:133:21: PERF402 Use `list` or `list.copy` to create a copy of a list
pyperf/tests/test_bench.py:351:13: PERF401 Use a list comprehension to create a transformed list
Found 9 errors.

% ruff rule PERF401

manual-list-comprehension (PERF401)

Derived from the Perflint linter.

What it does

Checks for for loops that can be replaced by a list comprehension.

Why is this bad?

When creating a transformed list from an existing list using a for-loop,
prefer a list comprehension. List comprehensions are more readable and
more performant.

Using the below as an example, the list comprehension is ~10% faster on
Python 3.11, and ~25% faster on Python 3.10.

Note that, as with all perflint rules, this is only intended as a
micro-optimization, and will have a negligible impact on performance in
most cases.

Example

original = list(range(10000))
filtered = []
for i in original:
    if i % 2:
        filtered.append(i)

Use instead:

original = list(range(10000))
filtered = [x for x in original if x % 2]

If you're appending to an existing list, use the extend method instead:

original = list(range(10000))
filtered.extend(x for x in original if x % 2)

@corona10
Copy link
Member

ditto: #183 (comment)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike this change. I don't think that these "micro optimizations" are worth it.

@vstinner
Copy link
Member

I suggest to ignore this recommendation instead of trying to address it just because ruff thinks that the code is slow.

@cclauss cclauss closed this Apr 29, 2024
@cclauss cclauss deleted the ruff-rules-PERF-and-C4 branch April 30, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants