5 ms·
The third method should be clearly the fastest one, the other two we are building a tuple then convert to a list
by charlieyu1 3y ago
The third method should be clearly the fastest one, the other two we are building a tuple then convert to a list
- masklinn 3y ago`-s` stands for "setup", the building of the tuple is only done once, and it is not part of the benchmark. All three versions only bench the construction of the list, two of them from a tuple, while the third is a list literal. However if you plug it into `dis` you'll see that it compiles to loading a const tuple and creating a list from that >>> dis.dis("[1, 2, 'a']") 0 0 RESUME 0 1 2 BUILD_LIST 0 4 LOAD_CONST 0 ((1, 2, 'a')) 6 LIST_EXTEND 1 8 RETURN_VALUE >>> dis.dis("[*t]") 0 0 RESUME 0 1 2 BUILD_LIST 0 4 LOAD_NAME 0 (t) 6 LIST_EXTEND 1 8 RETURN_VALUE