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 7978415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:29:17+00:00 2026-06-04T09:29:17+00:00

When iterating over a large array with a range expression, should I use Python’s

  • 0

When iterating over a large array with a range expression, should I use Python’s built-in range function, or numpy’s arange to get the best performance?

My reasoning so far:

range probably resorts to a native implementation and might be faster therefore. On the other hand, arange returns a full array, which occupies memory, so there might be an overhead. Python 3’s range expression is a generator, which does not hold all the values in memory.

  • 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-04T09:29:18+00:00Added an answer on June 4, 2026 at 9:29 am

    For large arrays, a vectorised numpy operation is the fastest. If you must loop, prefer xrange/range and avoid using np.arange.

    In numpy you should use combinations of vectorized calculations, ufuncs and indexing to solve your problems as it runs at C speed.
    Looping over numpy arrays is inefficient compared to this.

    (Something like the worst thing you could do would be to iterate over the array with an index created with range or np.arange as the first sentence in your question suggests, but I’m not sure if you really mean that.)

    import numpy as np
    import sys
    
    sys.version
    # out: '2.7.3rc2 (default, Mar 22 2012, 04:35:15) \n[GCC 4.6.3]'
    np.version.version
    # out: '1.6.2'
    
    size = int(1E6)
    
    %timeit for x in range(size): x ** 2
    # out: 10 loops, best of 3: 136 ms per loop
    
    %timeit for x in xrange(size): x ** 2
    # out: 10 loops, best of 3: 88.9 ms per loop
    
    # avoid this
    %timeit for x in np.arange(size): x ** 2
    #out: 1 loops, best of 3: 1.16 s per loop
    
    # use this
    %timeit np.arange(size) ** 2
    #out: 100 loops, best of 3: 19.5 ms per loop
    

    So for this case numpy is 4 times faster than using xrange if you do it right. Depending on your problem numpy can be much faster than a 4 or 5 times speed up.

    The answers to this question explain some more advantages of using numpy arrays instead of python lists for large data sets.

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

Sidebar

Related Questions

I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I have the following issue related to iterating over an associative array of strings
Is there a better shorter way than iterating over the array? int[] arr =
I'm iterating over a very large set of strings, which iterates over a smaller
the use case that I'm concerned with in this post involves iterating over a
I'm iterating over a large number of dom elements in Javascript, and I'd like
I am iterating over input elements using .each() and want to get class of
I'm iterating over a very large list of divs with WatiN (about 3000) and
I'm planning on building up a potentially large string by iterating over a collection
I'm trying to perform an iteration over a large set of entities to see

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.