I'm by no means an expert on python's internal work, but from my understanding so far, you would like to compare speed
for item in sizes: if(item == min(sizes)): count = count + 1
and
min_item = min(sizes) for item in sizes: if(item == min_item): count = count + 1
Now someone will correct me if I have any of this wrong, but
The python lists are variable and have no fixed length , and are treated as such, while in C the array has a fixed size. From this question :
Python lists are very flexible and can contain completely heterogeneous, arbitrary data, and can be added very efficiently, in an amortized time constant. If you need to shrink and grow your array efficiently and without hassle, this is the way to go. But they use a lot more space than C. arrays
Now take this example
for item in sizes: if(item == min(sizes)): new_item = item - 1 sizes.append(new_item)
Then the value of item == min(sizes) will differ at the next iteration. Python does not cache the resulting min(sizes) value, as it will violate the above example or require some logic to check if the list has been changed. Instead, it reaches you. min_item = min(sizes) defining min_item = min(sizes) , you essentially do the caching of the result yourself.
Now, since the array is a fixed size in C, it can find the min value with less overhead than the python list, so I think it has no problems in C (as well as C, which is a lower level language).
Again, I donβt quite understand the basic code and compilation for python, and Iβm sure that if you analyze the looping process in python, you will see that python repeatedly calculates min(sizes) , causing an extreme amount of lag. I would like to know more about python's internal work (for example, any methods cached in a loop for python, or everything computed again for each iteration?), So if anyone has more information and / or fixes, let me know !