4

When trying to test the speed of functions, I found that not all parts of my code work in Release mode. The same code works perfectly in Debug mode, though.

I'm using VC++ compiler with /O2 optimization.

Here's the cut out part, which doesn't work.

int main()
{
    boost::timer::auto_cpu_timer t;

    for(int i = 0; i < 1000000; i++)
        gcdb(i, 5);
    return 0;
}

The generated assembly in release mode, the code for the for loop is missing only in this mode.

int main()
{
000000013F8E1280  sub         rsp,88h  
000000013F8E1287  mov         rax,qword ptr [__security_cookie (013F8E7150h)]  
000000013F8E128E  xor         rax,rsp  
000000013F8E1291  mov         qword ptr [rsp+70h],rax  
    boost::timer::auto_cpu_timer t;
000000013F8E1296  lea         rcx,[t]  
000000013F8E129B  mov         edx,6  
000000013F8E12A0  call        boost::timer::auto_cpu_timer::auto_cpu_timer (013F8E2DA0h)  

    for(int i = 0; i < 1000000; i++)
        gcdb(i, 5);
    return 0;
000000013F8E12A5  lea         rcx,[t]  
000000013F8E12AA  call        boost::timer::auto_cpu_timer::~auto_cpu_timer (013F8E2810h)  
000000013F8E12AF  xor         eax,eax  
}

gcdb() is just a function for the finding the GCD of two numbers.

What could be causing this code skipping?

3
  • 2
    Compiler optimizes it out, is not a good reason?
    – Alok Save
    Jan 27, 2012 at 17:16
  • Can I keep it from optimizing out that part?
    – sks
    Jan 27, 2012 at 17:19
  • 1
    Try declaring a variable outside the loop and assigning result of gcdb() to it.
    – lapk
    Jan 27, 2012 at 17:22

2 Answers 2

8

What you're seeing here is a compiler optimization known as Dead Code Elimination.

When the compiler sees that the result of some code is not needed, it is free to eliminate it. This is a standard optimization employed by all modern compilers.

A work-around to keep the compiler from optimizing it out is to actually use the output in some way:

int main()
{
    boost::timer::auto_cpu_timer t;

    int sum = 0;

    for(int i = 0; i < 1000000; i++)
        sum += gcdb(i, 5);

    cout << sum << endl;
    return 0;
}

Related: How does GCC optimize out an unused variable incremented inside a loop?

3
  • 2
    @Ynau: The compiler is detecting that gcdb has no side effect, so it can be removed without altering the functionality of the program; similarly, the for loop can be removed. That's the purpose of optimisation: to improve the performance without altering the functionality.
    – MRAB
    Jan 27, 2012 at 17:25
  • Could you also run in debug mode, but perform debug without debugging? Jan 22, 2013 at 4:15
  • @JeremyTrifilo You could, but the numbers you get will be meaningless. Benchmarking a program without enabling optimizations is like timing Usain Bolt's 100m dash without telling him he's actually supposed to run.
    – Mysticial
    Jan 22, 2013 at 4:18
2

If the compiler can prove there are no side-effects and you don't use the result anywhere, it's free to prune that loop entirely.

Try summing the results and then returning the integer sum from main - that way there's an observable side-effect which should stop the optimizer getting too clever.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.