[Python] List vs generator comprehension speed with join function [duplicate]

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    So I got these examples from the official documentation.

    What exactly makes the first example (generator expression) slower than the second (list comprehension)?

    >>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
    0.3018611848820001
    >>> timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000)
    0.2727368790656328

    Continue reading...

Compartilhe esta Página