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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:54:34+00:00 2026-05-24T02:54:34+00:00

I have a loop that is my biggest time suck for a particular function

  • 0

I have a loop that is my biggest time suck for a particular function and I’d like to speed it up. Current, this single loop takes up about 400ms, while the execution for the rest of the function takes about 610ms.

The code is:

for ctr in xrange(N):
    list1[ctr] = in1[ctr] - in1[0] - ctr * c1
    list2[ctr] = in2[ctr] - in2[0] - ctr * c2
    list3[ctr] = c3 - in1[ctr]
    list4[ctr] = c4 - in2[ctr]

N can be anywhere from around 40,000 to 120,000, and is the length of all lists (in1, in2, listN) shown.

Does anyone know some Python tricks to speed this up? I already tried using map instead, as I know it tries to compile to more efficient code, but it was about 250ms slower.

Thanks

  • 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-05-24T02:54:34+00:00Added an answer on May 24, 2026 at 2:54 am

    Assuming that list1, list2, etc, all are numerical, consider using numpy arrays instead of lists. For large sequences of integers or floats you’ll see a huge speedup.

    If you go that route, your loop above could be written like this:

    ctr = np.arange(N)
    list1 = n1 - n1[0] - ctr * c1
    list2 = n2 - n2[0] - ctr * c2
    list3 = c3 - ctr
    list4 = c4 - ctr
    

    And as a full stand-alone example for timing:

    import numpy as np
    N = 100000
    
    # Generate some random data...
    n1 = np.random.random(N)
    n2 = np.random.random(N)
    c1, c2, c3, c4 = np.random.random(4)
    
    ctr = np.arange(N)
    list1 = n1 - n1[0] - ctr * c1
    list2 = n2 - n2[0] - ctr * c2
    list3 = c3 - ctr
    list4 = c4 - ctr
    

    Of course, if your list1, list2, etc are non-numerical (i.e. lists of python objects other than floats or ints), then this won’t help.

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

Sidebar

Related Questions

I have a loop that looks something like this while(condition){ read_some_data(source, buf, BUFSIZE); printf(buf);
I have a loop that looks like this def slow_loop(array) array.each_with_index do |item, i|
Say I have a loop that looks like this: for(int i = 0; i
I have a loop that looks like this: for (int i = 0; i
I have a loop that runs through a variety of websites and I'd like
I have a loop that takes file name from a listbox, performs a system()
Stuck with this particular issue. I have code that get lots of pages from
I have a loop that's reading over a text file. Each time the loop
In my application i have a loop that executes about 1000 times, inside it
This is driving me insane now. I have a loop that creates buttons. Each

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.