Why is it unlikely / unlikely to show performance improvements?

I have many validation checks in the code where the program crashed if any verification failed. Thus, all checks are more unlikely.

if( (msg = newMsg()) == (void *)0 )//this is more unlikely { panic()//crash } 

So, I used an unlikely macro that the compiler tells you in branch prediction. But I have not seen an improvement with this (I have some performance tests). I am using gcc4.6.3.

Why is there no improvement? Is it because there is no other case? Should I use any optimization flag when creating my application?

+5
source share
2 answers

Should I use any optimization flag when creating my application?

Absolutely! Even the optimization, rotated to the lowest level, -O1 for GCC / clang / icc, is likely to surpass most of your optimization efforts. Basically, for free, so why not?

I am using gcc4.6.3.

GCC 4.6 is old. You should consider working with modern tools, unless you are limited.

But I have not seen an improvement with this (I have some performance tests).

You have not seen any visible performance improvements, which is very common when working with such micro-optimizations. Unfortunately, achieving modern improvements is not so easy with today's equipment: this is because we have faster (incredibly fast) components than before. Thus, saving cycles is not as smart as before.

Despite the fact that it is worth noting that consecutive microoptimizations can make your code much faster, as in tight loops. Avoiding stalls, incorrect industry predictions, maximizing cache usage make a difference when processing pieces of data. And SO the most voted question clearly shows that.

This is even stated in the GCC manual:

- Built-in function: long __builtin_expect (long exp, long c)
You can use __builtin_expect to provide branch prediction information to the compiler. In general, you should prefer to use the actual feedback for this (-fprofile-arcs), as programmers are known to poorly predict how their programs actually execute. However, there are applications in which this data is difficult to collect.

(my emphasis)

+3
source

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


All Articles