Namespaces
Variants
Actions

Talk:cpp/language/attributes/likely

From cppreference.com

This page contains a detailed example with a possible output of running that code, that shows a significant runtime improvement caused by using [[likely]]. However, the numbers seem quite optimistic, and I was not able to reproduce this effect at all (I tried with both gcc and clang on an x86). Does anyone have any insights on how to get such results? E.g. some compiler + processor combination that could produce it?

For reference, it was added in this edit by Space Mission: https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes/likely&diff=125917&oldid=124529


Tomerv (talk) 10:34, 8 June 2022 (PDT)

my PC (Xeon W-2145) is fairly consistently showing this difference, with gcc 11.3:
cubbi@gummadoon~$ g++ -o test test.cc -O3
cubbi@gummadoon~$ ./test | tail -3
Time: 0.345119 sec (with attributes:)
Time: 0.421355 sec (without attributes)
Time: 0.204150 sec (std::cos)
cubbi@gummadoon~$ ./test | tail -3
Time: 0.342668 sec (with attributes:)
Time: 0.421949 sec (without attributes)
Time: 0.204974 sec (std::cos)
cubbi@gummadoon~$ ./test | tail -3
Time: 0.344669 sec (with attributes:)
Time: 0.442096 sec (without attributes)
Time: 0.206048 sec (std::cos)
and I even rotated the lines around in the source in case it's some cache warmup, and the numbers persisted:
Time: 0.223253 sec (std::cos)
Time: 0.418761 sec (without attributes)
Time: 0.325539 sec (with attributes:)
so.. works for me --Cubbi (talk) 14:08, 8 June 2022 (PDT)
I ran it on visual studio for funsies, and I got
Time: 1.969911 sec (with attributes:)
Time: 1.979593 sec (without attributes)
Time: 0.163588 sec (std::cos)
and in GCC 11.1 on my ubuntu WSL
Time: 0.517465 sec (with attributes:)
Time: 0.454532 sec (without attributes)
Time: 0.237524 sec (std::cos)
For extra funsies, GCC 9.4 gives me
Time: 1.228546 sec (with attributes:)
Time: 0.654401 sec (without attributes)
Time: 0.238529 sec (std::cos)
Suffice to say that mileage here is very varying --Ybab321 (talk) 02:42, 9 June 2022 (PDT)
Thank you both for taking the effort to check. Cubbi, I think that -O3 is the key here:
-O2:
Time: 0.796730 sec (with attributes)
Time: 0.706116 sec (without attributes)
Time: 0.298919 sec (std::cos)
-O3:
Time: 0.443033 sec (with attributes)
Time: 0.553174 sec (without attributes)
Time: 0.276254 sec (std::cos)
Ybab321, did you compile with -O3?
Tomerv (talk) 09:44, 13 June 2022 (PDT)
I compiled with -O2 as well, to my surprise -O3 does replicate the desired results on GCC 11.1!
Time: 0.353941 sec (with attributes)
Time: 0.428078 sec (without attributes)
Time: 0.219131 sec (std::cos)
On GCC 9.4 there's no significant difference (unsurprisingly). Guess I need to show -O3 some more respect in the future --Ybab321 (talk) 14:02, 13 June 2022 (PDT)
According to the GCC status they added [[likely]]/[[unlikely]] attributes support in GCC-9, but empirically, the optimal code generation was ready since GCC-10. Godbolt (with this example) is quite expressive on this.) --Space Mission (talk) 15:36, 13 June 2022 (PDT)