On Sat, Oct 16, 2021 at 12:21 PM David Mertz, Ph.D. <david.me...@gmail.com>
wrote:

> I'm not making any claims about tuple creation speed vs. list creation on
> microbenchmarks. It might we'll be 10% faster to create a million item
> tuple than a million item list. Or maybe the opposite, I don't know.
>

The thing to know about this (for everyone) is that creating a new tuple of
known size requires a single allocation while creating a new list always
requires two allocations. Where this really makes a difference is not when
you have a million items but when you create thousands or millions of
objects of modest size -- for lists it takes twice as many allocations.

If the number of items is not known there are different strategies
depending on what API you use; interestingly, the strategy deployed by
PySequence_Tuple() has a comment claiming it "can grow a bit faster than
for lists because unlike lists the over-allocation isn't permanent."

Finally, the bytecode generated for (*a, *b) creates a list first and then
turns that into a tuple (which will be allocated with the right size since
it's known at that point).

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MNQBV6ORMV6DHUV44M524TCJHCLOBKI5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to