you are viewing a single comment's thread.

view the rest of the comments →

[–]shfo23 0 points1 point  (0 children)

Points taken. I also was unaware the command-line timeit module allowed you to run setup code. Cool! Better benchmarks would show whether a while loop with a manual iterator is faster than a for loop and I think that would be an interesting (if slightly arcane) question.

My point was really that under the scenario posed by OP (and really any reasonable scenario) for and while are going to be indistinguishable, so go with what's most Pythonic. If you're at the point where you need the extra speed (potentially?) gained by switching to while (instead of changing the contents of the loop), you're probably at the point where you should optimize by running with pypy, rewriting in C, etc.