1. 程式人生 > >有關likely和unlikely(轉)

有關likely和unlikely(轉)

紅色部分,原帖中沒有。

在linux中判斷語句經常會看到likely和unlikely,例如:

if(likely(value)){

}

else{

}

簡單從表面上看if(likely(value)) == if(value),if(unlikely(value)) == if(value)。

也就是likely和unlikely是一樣的,但是實際上執行是不同的,加likely的意識是value的值為真的可能

性更大一些,那麼執行if的機會大,而unlikely表示value的值為假的可能性大一些,執行else機會大一些。

加上這種修飾,編譯成二進位制程式碼時likely使得if後面的執行語句緊跟著前面的程式,unlikely使得else後

面的語句緊跟著前面的程式,這樣就會被cache預讀取,增加程式的執行速度,likely和unlikely的實現在

include/linux/compiler.h中:

在fedora10下,目錄是/usr/src/kernels/版本號/include/linux/compiler.h

  9 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
  10 #define __builtin_expect(x, expected_value) (x)
  11 #endif
  12 
  13 #define likely(x) __builtin_expect((x),1)
  14 #define unlikely(x) __builtin_expect((x),0)

__builtin_expect是gcc的一個預處理命令,其解釋如下:

long __builtin_expect (long exp, long c)
You may use __builtin_expect to provide the compiler with branch prediction
information. In general, you should prefer to use actual profile feedback for this

(‘-fprofile-arcs’), as programmers are notoriously bad at predicting how their
programs actually perform. However, there are applications in which this data is
hard to collect.
The return value is the value of exp, which should be an integral expression. The
value of c must be a compile-time constant. The semantics of the built-in are that it
is expected that exp == c. For example:
if (__builtin_expect (x, 0))
foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since
you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1))
error ();
when testing pointer or floating-point values.