1

I got two almost same loop, but with remarkable difference in performance, both tested with MSVC2010, on system ~2.4 GHZ and 8GB RAM

Below loop take around 2500 ms to execute

for (double count = 0; count < ((2.9*4/555+3/9)*109070123123.8); count++)
;                   

And this loop execute in less then 1 ms

for (double count = ((2.9*4/555+3/9)*109070123123.8); count >0; --count)
;

What making such huge difference here? One got post increment and other using pre-increment can it result in such huge difference?

3
  • If you enable optimization, booth loops should collapse to NOTHING, and thus take 0 time. Don't try to measure performance with optimization turned off! Commented Apr 6, 2013 at 10:39
  • -1 there is no point in talking about performance when not using at least the most trivial optimisations. Commented Apr 6, 2013 at 10:59
  • 1
    1ms is not a change in performance. You have to mention what you achive with it i.e. MFLOPS (Standard array) or MLUPS (Grid Calculation). Lets consider how the for loop works. Step1: Assign Value to Variable. Step2: Check If condition is true and Execute Block of Code Step3: Increment Step4: Repeat Step2 and Step3. In the first loop you need to calculate the value of expression for every iteration and in second loop you do the calculation only once. Commented Apr 8, 2013 at 0:06

2 Answers 2

5

You're compiling without optimizations, so the comparison is futile. (If you did have optimizations on, that code would just be cut out completely).

Without optimization, the computation is likely executed at each iteration in the first loop, whereas the second loop only does the computation once, when it first initializes count.

Try changing the first loop to

auto max = ((2.9*4/555+3/9)*109070123123.8);
for (double count = 0; count < max; count++)
;   

and then stop profiling debug builds.

Sign up to request clarification or add additional context in comments.

Comments

2

In the first loop count < ((2.9*4/555+3/9)*109070123123.8) is computed every time round the loop where as in the second count = ((2.9*4/555+3/9)*109070123123.8) is calculated once and decremented each time round the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.