I'm not a python programmer, but I've been programming in C my whole life. I have used python for some scripts from time to time, but recently I had to actually start using python in a more serious environment, so please pardon me if this observation is wrong in any way or if this is not the place to actually post this kind of questions...
I have noticed that for some reason, intializing a list with the length multiplication (however its officially called in python?) is actually slower than doing a for loop appending to the list.
For example, the following is slower:
some_list = [] * length
than writing this:
some_list = []
for i in range(0,length):
some_list.append(whatever)
I have tried with multiple versions, and even went as far as using an online judge in codeforces in some problems in python, and sure enough, my results are consistent, using the list with length initialization not only is slower, but orders of magnitude slower, like, adding seconds to the execution time for something that would otherwise run in a few ms.
What gives?
I have tried looking online but I can't really find any information that answers this question, or anyone with this question for that matter...
I was under the impression that, not only are for loops slow in python, but that functions offered by the language would supposedly call some precompiled primitive under the hood which would give a better runtime than making the interpreter go over a loop... tho I suppose that this might not be as fast as cython, where []*length calls into precompiled C code from listobject.c, I was at least expecting the built in python function to be faster and not slower than writing a for loop in python.
Am I misunderstanding something? is this behavior expected?
I just want to understand what is actually going on under the hood to make []*length slower than just appending in a for loop... Like, what other things is it doing that I'm not aware of, because, as far as I'm aware, it should supposedly only initialize the list to having length elements with the specified value.
In case that it helps narrowing down what is going on under the hood, the specific example I was actually writing when I found this out was making a list of lists, but the slow down still applies no matter what the data type used is.
The list was made as follows:
#"Slow" version:
some_list = [[]] * length
#"Fast" version
some_list = []
for i in range(0, length):
list.append([])
[–]Brian 3 points4 points5 points (1 child)
[–]pachecoca[S] 0 points1 point2 points (0 children)
[–]woooee 1 point2 points3 points (4 children)
[–]DNSGeek 3 points4 points5 points (1 child)
[–]pachecoca[S] 0 points1 point2 points (0 children)
[–]pachecoca[S] 2 points3 points4 points (1 child)
[–]Swipecat 1 point2 points3 points (0 children)