Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

visual c++ branching and optimization question?

I have a couple of questions regarding optimization of c++ code for visual c++ (2010) for intel platform having to do with IF's and branches.

If I have the statement

int x = (someBooleanCondition) ? 1 : 0;

Does the compiler/intel make use of a conditional load instruction or does it do a series of branches to make the correct assignment? I know branches are inefficient and I've done work on DSPs that have a conditional load instruction to help avoid them, but I don't know if the pentium or visual c++ have/use them.

The next question is if I have something like

if(someBooleanCondition) {

// code that is not normally run. maybe <1% of the time this condition is true

}

What is the optimum way to structure this so that 99+ % of the time it will not do any branches. Essentially, I'd want it to test the condition, then branch if the condition is true to some other place in the code, then branch back after those statements are executed.

Will it only do that if I have a procedure call? Sometimes I'd like to have access to local variables in there and don't want to go through the hassle of putting in the overhead for a procedure call.

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favourite answer

    I don't have VC++2010 handy but hopefully the following generalizations will be helpful:

    1. SETcc and CMOVcc instructions were introduced in the Pentium Pro but Intel still recommends you query the CPU flags to check for their availability (blah). Make sure you're compiling for a sufficiently high level of processor then verify with a disassembler such as DUMPBIN what it's doing.

    2. MSVC++'s recommended, supported way of hinting is through profile-guided optimization. Instrument the program, run it on realistic data, then recompile using the profile info as a guide. If you're feeling lucky, you could try things like

    if (!cond) { } else {

    // rare stuff

    }

    or

    int func() {

    //...

    if (cond)

    goto eek;

    ok:

    //...

    return;

    eek:

    //rare stuff

    goto ok;

    }

    but, again, verify with a disassembler that the compiler isn't being "helpful" and putting the cold code back inline. Also consider that if your function fits entirely in cache, which it probably does if branches will be that hot, the taken branch may be less painful than you think.

    Consider inline assembly so that you know for sure. Also consider using alternate compilers such as Intel's or GCC, if you can. They take explicit pragmata to indicate likely and unlikely branches, in case the meager information provided by compile-time analysis is wrong.

    Good luck!

  • ?
    Lv 7
    1 decade ago

    Whether you write int x = (someBooleanCondition) ? 1 : 0; or if(someBooleanCondition) { x = 1; }, MSVC and all other modern compilers that I've checked will use the CMOV instruction where available.

    Low-level optimization is not about re-ordering branches in your code -- if you think you need that, switch to assembly language because the C++ compilers can and will reorder or even eliminate the branches as they see fit (as long as no externally visible side-effects are programmed in those branches, such as volatile-qualified object access -- that's the whole reason we *have* the keyword volatile in C++). It is about algorithm analysis, cache locality, page faults, etc.

Still have questions? Get answers by asking now.