Is it worth using binary operators to gain performance?

Asked

Viewed 142 times

6

I have the following situations:

if (1 & 1){}

and

if (1 == 1){}

According to what I’ve learned, working with bitwise Operators causes a much better performance in the program, with this I came up with some questions:

  • Is the performance in the first situation really better than in the second? Why?

  • There are situations where using a binary operator will cause worse performance?

  • 3

    That’s the kind of performance concern you should not have. It is more productive to worry about real performance bottlenecks in real code, and usually this involves changing the algorithms used. Use a bitwise operator instead of a == will only make your code less readable and should not be done unless you are sure it is worth in your specific situation.

  • I disagree with the friend above, first that the content of the question and about performance not on bottlenecks, performance of the codes affect yes in response time, not that it is something significant, more in my opinion it is worth more performace than bunito code. Just for the record I’m not a professional in the area so I might be talking shit

  • 3

    @Arsomnolasco My comment has to do with what Maniero just said. Performance issues should not be treated in an abstract way, it depends on the specific context.

1 answer

6


In many cases, yes, in many cases, no. In general the recommendation is not to use them until the performance is not good enough or only the manipulation is suitable for the desired semantics, even because current compilers usually exchange the arithmetic or relational operation for bit manipulation, whenever it is advantageous. But it does not solve all cases in the best way. Most solve better than the programmer.

In such artificial examples, the code will actually be reduced to nothing. Only the header and footer function (in some cases not even that):

Header/footer de função

Only testing in the specific situation in the specific implementation, in the specific architecture, to see if it gets faster or not.

It’s useful to do this when you need performance and know that the compiler is not optimizing properly.

It is also useful if the semantics is of manipulating numbers at a lower level, since the intention is really to mess with bits and not a high-level number.

In some cases the processor can optimize the execution, even beyond this operation, depending on the instruction used in the code, but only the compiler knows what instruction he used.

So also goes a little taste in most cases.

If you use it wrong it could be worse.

I didn’t even mention the difference between processors. And it’s not just family, it even varies according to model.

So it depends, you can’t answer something that even has a definitive answer.

The fact of generating different codes does not mean that one is faster than the other since each instruction has a different execution time and depends on the general context and the execution environment. It is possible to measure, but it takes work. The compiler version and settings can change the generated code.

  • I missed "Why?" Could you explain it to me?

  • @Francisco edited to try to "answer"

  • PGO (Profile Guided Optimization) can also change the branch to an instruction without branches if the first is checked as slow!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.