Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:11:05+00:00 2026-06-03T08:11:05+00:00

I have a script that evokes a for loop with a set number of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:11:07+00:00Added an answer on June 3, 2026 at 8:11 am

    Some local test results:

    >>> timeit.timeit('for i in repeat(None, 1000): pass', setup='from itertools import repeat', number=100000)
    1.94118924332561
    >>> timeit.timeit('for i in xrange(1000): pass', number=100000)
    2.5231991775491025
    >>> timeit.timeit('for i in range(1000): pass', number=100000)
    3.9302601308266816
    >>> timeit.timeit('for i in r: pass', setup='r = [None] * 1000', number=100000)
    2.0900103923822684
    >>> timeit.timeit('for i in r: pass', setup='r = range(1000)', number=100000)
    2.2248894063351656
    >>> timeit.timeit('for i in r: pass', setup='r = xrange(1000)', number=100000)
    2.9105822108675823
    

    You can’t use caching for the itertools.repeat one, since that iterator behaves like a generator (you can only “read” the values once, and then they’re gone).

    Repeating 1 or something similar might be infinitesimally faster because the name None no longer has to be looked up, but any such performance benefit is lost in the noise of random variation in test results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that uses a simple while loop to display a progress
I have a script that takes a number of filenames, and then serializes the
I have a script that is automating author re-writes on a number of git
I have a script that is supposed to read from the database and return
I have a script that checks something on my PC every 5 minutes and
I have a script that connects to SQL Server 2005 default instance. But I'm
I have a script that will convert a text file into a resource file,
I have a script that generates DDL scripts to define materialized views for a
I have a script that generates images from text using PHP. It's working fine
I have a script that runs every two minutes for a Tweet-getter application. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.