The Fastest Way to Loop in Python - An Unfortunate Truth
Why Stack Overflow recommends it
Stack Overflow users link it when explaining why plain Python loops are slow and why built-ins such as sum(range(N)) or vectorized approaches beat manual iteration.
Recommended by 1 people across 1 different months, 2021 to 2022.
What it is
James Murphy of mCoding benchmarks several ways to do the same looping task in Python, including for loops, while loops, and built-in functions like sum(range(N)). He shows that pushing the iteration into C-implemented built-ins is far faster than looping in pure Python. The video is a short, code-driven explainer with timings.
What people said
Recommends flowerboy · 2022-09 · Stack Overflow · on “Faster Loop in a multi-index pandas dataframes”
afaik there's not really a way to speed up looping in python, it doesn't have anything to do with pandas either just using python for- and while-loops is rather slow. so you would have to find another way to go about it if you need speed. see this: [this video]
Cites Yoshikage Kira · 2021-05 · Stack Overflow · on “Find indexes of common items in two python lists”
A slight improvement, but still weird. I am not sure who told you the `for loop` is least efficient way to do things. It only tells part of the story. Hell even the `intersect` function uses `for loop` behind the scenes. [LINK] Also, technically speaking, for loop is the fastest way to iterate in python. This is because the iteration part is handled by C. Which is way faster than python. Fasted way to loop in Python [this video]
Cites fbence · 2021-10 · Stack Overflow · on “Why is `np.sum(range(N))` very slow?”
I saw a video [this video] about speed of loops in python, where it was explained that doing sum(range(N)) is much faster than manually looping through range and adding the variables together, since the former runs in C due to built-in functions being used, while in the latter the summation is done in (slow) python. I was curious what happens when adding numpy to the mix. As I expected np.sum(np.arange(N)) is the fastest, but sum(np.arange(N)) and np.sum(range(N)) are even slower than doing the naive for loop. Why is this? Here's the script I used to test, some comments about the supposed cause of slowing done where I know (taken mostly from the video) and the results I got on my machine (python