Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I replace an if-statement with AND?

My prof once said, that if-statements are rather slow and should be avoided as much as possible. I’m making a game in OpenGL, where I need a lot of them.
In my tests replacing an if-statement with AND via short-circuiting worked, but is it faster?

bool doSomething();

int main()
{

  int randomNumber = std::rand() % 10;

  randomNumber == 5 && doSomething();

  return 0;
}

bool doSomething()
{
    std::cout << "function executed" << std::endl;

    return true;
}

My intention is to use this inside the draw function of my renderer. My models are supposed to have flags, if a flag is true, a certain function should execute.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

if-statements are rather slow and should be avoided as much as possible.

This is wrong and/or misleading. Most simplified statements about slowness of a program are wrong. There’s probably something wrong with this answer too.

C++ statements don’t have a speed that can be attributed to them. It’s the speed of the compiled program that matters. And that consists of assembly language instructions; not of C++ statements.

What would probably be more correct is to say that branch instructions can be slow (on modern, superscalar CPU architectures) (when the branch cannot be predicted well).

randomNumber == 5 && doSomething();

An if-statement is often compiled into a program that uses a branch instruction. A short-circuiting logical-and operation is also often compiled into a program that uses a branch instruction. Replacing if-statement with a logical-and operator is not a magic bullet that makes the program faster.

If you were to compare the program produced by the logical-and and the corresponding program where it is replaced with if (randomNumber == 5), you would find that the optimiser sees through your trick and produces the same assembly in both cases.

My models are supposed to have flags, if a flag is true, a certain function should execute.

In order to avoid the branch, you must change the premise. Instead of iterating through all models, checking flag, and conditionally calling a function, you could create a sequence of all models for which the function should be called, iterate that, and call the function unconditionally -> no branching. Is this alternative faster? There is certainly some overhead of maintaining the data structure and the branch predictor may have made this unnecessary. Only way to know for sure is to measure the program.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading