Replies: 2 comments 1 reply
-
But how often is this used as a performance-critical code fragment? |
Beta Was this translation helpful? Give feedback.
-
Note that A more appropriate observation would be comparing the difference between We could, perhaps, introduce a |
Beta Was this translation helpful? Give feedback.
-
Building a list is slower than building a tuple. When investigating why this is, I found out the following: compare the following three constructions: a simple list
a=[1,2,3,4,5]
, a tuplea=(1,2,3,4,5)
and finally a list constructed via a tuplea=[(1,2,3,4,5)]
.On my system running
results in
So constructing a list from a tuple is faster than constructing the list directly.
The bytecode generated by the three constructions is:
list
list from tuple
tuple
Can we modify the bytecode generation for
list
such that we build the list in one step, instead of extending an empty list?Note: there is more happening that might explain the different in the construction times. Both list and tuple seem to use freelists, but the implementation looks different (at first sight). Also the list and tuple have different behavior for short lengths, e.g. compare
Beta Was this translation helpful? Give feedback.
All reactions