Syntax syntax or binary optimized syntax?

Take a simple example of two lines supposedly doing the same thing:

if (value> = 128 || value <0) ...

or

if (value and ~ 127) ...

Say, "If it's expensive in loops of thousands of iterations, is it better to use traditional C syntax or better to find binary optimized if possible?

+4
source share
4 answers

I would use the first statement with traditional syntax, as it is more readable. You can break your eyes with the second statement.

Pay attention to programmers who will use the code after you.

+3
source

In 99 cases out of 100, do something that is more readable and expresses your intention better.

+3
source

In theory, compilers will do this kind of optimization for you. In practice, they cannot. This specific example is a bit subtle because the two are not equivalent unless you make some assumptions about value and whether signed arithmetic 2 is add-on on your target platform.

Use what you find more readable. If and when you have evidence that the performance of this particular test is critical, use what gives you the best performance. Personally, I will probably write:

 if ((unsigned int)value >= 96U) 

because it is more intuitive for me, and most likely I worked with most compilers.

+1
source

It depends on how / where the check is. If the test is performed once during program startup to check the command line parameter, the performance issue is completely debatable, and you should use all that more naturally.

On the other hand, if the check was inside some kind of inner loop that happens millions of times per second, then that might matter. But do not think it will be better; you have to create both versions and their time to see if there is any measurable difference between them.

+1
source

Source: https://habr.com/ru/post/1309626/


All Articles