I have a script that evokes a “for loop” with a set number of iterations (1000) over a million times. I’ve read the range() vs xrange() threads, and I am aware that I do not want to do something like this.
for o in xrange(1000000):
for i in range(1000): #Definitely do not want
pass
Instead, I wish to create an object that contains 1000 elements and then use that to constantly iterate.
Method 1:
iterate=range(1000)
for o in xrange(1000000):
for i in iterate: #<---
pass
Method 2:
for o in xrange(1000000):
for i in xrange(1000): #<----
pass
I was wondering which method would give better performance inside the “for loop.” Thank you.
Edit: Sorry. I believe I was unclear. My problem is whether I should make this inner loop I’m evoking use a list already created or use the xrange() for better performance.
Some local test results:
You can’t use caching for the
itertools.repeatone, since that iterator behaves like a generator (you can only “read” the values once, and then they’re gone).Repeating
1or something similar might be infinitesimally faster because the nameNoneno longer has to be looked up, but any such performance benefit is lost in the noise of random variation in test results.