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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:27+00:00 2026-05-22T18:19:27+00:00

When comparing the following code samples in python (the 300 is just an example;

  • 0

When comparing the following code samples in python (the 300 is just an example; in practicality, assume there are 10,000,000 items that we need to loop through):

sum = 0
for i in xrange(0, 300):
    sum += i

Vs.

sum = 0
for i in xrange(0, 100):
    sum += i
for i in xrange(100, 200):
    sum += i
for i in xrange(200, 300):
    sum += i

Are both versions the same in terms of running time? I have a problem where I have a huge vector (say, 10,000 items). I don’t necessarily have to loop through all of it, unless some conditions are met (these conditions can only be assessed when the first loop — which using the first 100 items — is done).

Then, I should continue the looping only if some conditions are met (these conditions can only be assessed when the next 100 items are examined).

Then, I should continue the looping only if some conditions are met (these conditions can only be assessed when the next 100 items are examined).

Same story, until I reach the end of the vector…………….

Given the fact that I can solve my problem in a modular way, by examining points:

{0, 100}
{100, 200}
{200, 300}
……..
{9900, 10,000}

All I am trying is to get a sense whether the second approach is as efficient as the first one. Thanks.

note: the sum is used here for simplicity. In practice, the running time of the mathematical operations that will be used is significantly greater. That’s why I am trying to find ways to optimize the running time by examining different coding approaches…

  • 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-22T18:19:28+00:00Added an answer on May 22, 2026 at 6:19 pm

    What kind of improvement were you hoping to see?

    timeit will help you observe the difference:

    $ python -m timeit 'sum = 0
    > for i in xrange(0, 300):
    >     sum += i'
    10000 loops, best of 3: 21.8 usec per loop
    
    $ python -m timeit 'sum = 0
    > for i in xrange(0, 100):
    >     sum += i
    > for i in xrange(100, 200):
    >     sum += i
    > for i in xrange(200, 300):
    >     sum += i'
    10000 loops, best of 3: 22.8 usec per loop
    

    Personally, I would go for:

    $ python -m timeit 'sum(xrange(300))'
    100000 loops, best of 3: 5.34 usec per loop
    

    This example is perhaps not the best way to assess whether you’ll be able to optimise your actual code, but it does demonstrate how you can test a small snippet for its running time.

    Just remember what Donald Knuth said about optimisation 😉

    Here’s a way you could checkpoint your iteration and break out if required:

    >>> for index, item in enumerate(vect):
    ...     results = do_stuff_with(item)
    ...     if index % 100 == 0 and should_we_stop(results):
    ...         break
    

    What would be even better is if you could have do_stuff_with() return a tuple indicating whether it has finished, then you can check every iteration whether you’re done, rather than wait:

    >>> for item in vect:
    ...     finished, results = do_stuff_with(item)
    ...     if finished:
    ...         break
    

    Just to reiterate (no pun intended) it’s very hard to say how (or even whether) to optimise your actual code without actually seeing it!

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

Sidebar

Related Questions

I am in the following situation: I am writing code for a kernel that
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
Comparing string in C# is pretty simple. In fact there are several ways to
Possible Duplicate: Reference: Comparing PHP's print and echo Is there any major and fundamental
I have trouble comparing 2 double in Excel VBA suppose that I have the
I am trying to tidy up some legacy code. The following is a cut
Disclaimer: I'm not very good at Java and just comparing read/writer locks between C#
I have small doubt in CoVariance and ContraVariance.. See the Following Code.. interface IGetElement<out
I'm trying to compile the following code. import Data.List import Data.Ord import qualified Data.MemoCombinators
The last Unit Test of in the following code fails, probably because the ==

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.