Skip to content

[Doc] reimplemented-starmap does not mention performance #7771

@Avasam

Description

@Avasam

https://github.com/dosisod/refurb/blob/master/docs/checks.md#furb140-use-starmap mentions performance.

https://docs.astral.sh/ruff/rules/reimplemented-starmap/ does not

My local tests seem to agree:

from itertools import starmap
from timeit import timeit

def foo(a: int, b: str): pass

big_list = ["*"] * 99

def test_generator_comprehension():
    return (
        foo(index, value) for index, value
        in enumerate(big_list)
    )

def test_list_comprehension():
    return [
        foo(index, value) for index, value
        in enumerate(big_list)
    ]

def test_generator_starmap():
    return starmap(
        foo,
        enumerate(big_list),
    )

def test_list_starmap():
    return list(
        starmap(
            foo,
            enumerate(big_list),
        ),
    )

print("test_generator_comprehension", timeit(test_generator_comprehension))
print("test_list_comprehension", timeit(test_list_comprehension))
print("test_generator_starmap", timeit(test_generator_starmap))
print("test_list_starmap", timeit(test_list_starmap))

Python 3.9.13:

test_generator_comprehension 0.3537939
test_list_comprehension 9.4948082
test_generator_starmap 0.22116740000000057
test_list_starmap 6.5608599000000005

Python 3.11.2 (surprisingly the generator comprehension is even slower in Python 3.11, despite the proclaimed comprehension speed improvements, list comprehension checks out):

test_generator_comprehension 0.46688559994800016
test_list_comprehension 8.789011800021399
test_generator_starmap 0.24132720002671704
test_list_starmap 7.120547099970281

Scales similarly with a list of ~100 or ~1million items (the list examples takes forever though)

I think it would be important to mention as well. It is not insignificant (a little bit for list, ~2x for generators) and the doc makes it sounds like a simple stylistic change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions