Which is more costly for current processors: arithmetic or notation?

20-30 years ago, arithmetic operations like division were one of the most expensive operations for processors. The significant performance gains were saved by one section in a chunk of re-named code. But processors today have fast arithmetic operations and since they rely heavily on instruction pipelining , conventions can break efficient execution. If I want to optimize my code for speed, do I prefer arithmetic in favor of conditional notation?

Example 1

Suppose we want to implement modulo operations n

. Which will be better:

int c = a + b;
result = (c >= n) ? (c - n) : c;

      

or

result = (a + b) % n;

      

?

Example 2

Let's say we convert 24-bit signed numbers to 32-bit. Which will be better:

int32_t x = ...;
result = (x & 0x800000) ? (x | 0xff000000) : x;

      

or

result = (x << 8) >> 8;

      

?

+3


source to share


3 answers


All the low hanging fruit have already been picked and pickled by the compiler authors and the guys who build the hardware. If you are the kind of person who has to ask such a question, you will hardly be able to optimize something manually.

Whereas 20 years ago it was possible for a relatively competent programmer to make some optimizations down to build, it is now the domain of target architecture experts; In addition, optimization requires not only knowledge of the program, but also knowledge of the data that it will process. It all comes down to heuristics, testing under different conditions, etc.



Simple productivity questions no longer have simple answers.

+1


source


If you want to optimize for speed, you should simply tell your compiler to optimize for speed. Modern compilers will generally outperform you in this area.

I have sometimes wondered at trying to link assembly code to source code for this very reason.



Optimize your source code for readability and let the compiler do what it does best.

+2


source


I expect the former to perform better in example # 1. The compiler will probably apply some bit of a trick to avoid branching. But you are using the knowledge that it is extremely unlikely that the compiler can infer: namely, that the sum is always in a range [0:2*n-2]

, so one subtraction is sufficient.

For example # 2, the second method is faster on modern processors and is easier to follow. A sensible comment would be appropriate in both versions. (I wouldn't be surprised if the compiler converts the first version to the second.)

+2


source







All Articles