We can do numeric iteration like:
for i in xrange(10):
print i,
and in C-style:
i = 0
while i < 10:
print i,
i = i + 1
Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?
PS. I’m from C++ planet and pretty new on Python one.
I am sure the
whileversion is slower. Python will have to lookup the add operation for the integer object on each turn of the loop etc, it is not pure C just because it looks like it!And if you want a pythonic version of exactly the above, use:
Edit: My timings look like this. This is just a silly running loop without printing, just to show you what writing out “i += 1” etc costs in Python.