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...