亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

likely()與unlikely()

系統 2058 0

he gcc C compiler has a built-in directive that optimizes conditional branches as either very likely taken or very unlikely taken. The compiler uses the directive to appropriately optimize the branch. The kernel wraps the directive in very easy-to-use macros, likely() and unlikely() .

For example, consider an if statement such as the following:

    
      if (foo) {

        /* ... */

}
    
  

To mark this branch as very unlikely taken (that is, likely not taken):

    
      /* we predict foo is nearly always zero ... */

if (unlikely(foo)) {

        /* ... */

}
    
  

Conversely, to mark a branch as very likely taken:

    
      /* we predict foo is nearly always nonzero ... */

if (likely(foo)) {

        /* ... */

}
    
  

You should only use these directives when the branch direction is overwhelmingly a known priori or when you want to optimize a specific case at the cost of the other case. This is an important point: These directives result in a performance boost when the branch is correctly predicted, but a performance loss when the branch is mispredicted. A very common usage for unlikely() and likely() is error conditions. As one might expect, unlikely() finds much more use in the kernel because if statements tend to indicate a special case.

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? from Linux Kernel Development Second Edition

?

?

---------------------------------------------------------------------------------------------

?

在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中:

            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.

轉自:http://www.cnblogs.com/yangzd/archive/2010/09/27/1837202.html

likely()與unlikely()


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久频这里精品99香蕉久网址 | 黑人巨大vs日本人优 | 久久精品视频9 | 欧美一级成人免费大片 | 青草娱乐极品免费视频 | 欧美jizz18性欧美 | 亚洲黄色大片 | 私人影院免费 | 亚洲日韩在线视频 | 99视频一区| 老妇毛片久久久久久久久 | 亚洲 欧美 bt | 国产精品视频观看 | 久久免费播放 | 亚洲视频 中文字幕 | 久操香蕉 | 欧美社区| 一区二区三区日韩精品 | 99久久99久久 | 四虎免费看黄 | 97在线观看完整免费 | 美国黑人特大一级毛片 | 国产激情视频一区二区三区 | 国产一区二区三区免费播放 | 一区二区三区美女视频 | 爱操综合 | 亚洲精品在线视频观看 | 成年黄页免费大全网站 | 国产精品嫩草研究所永久网址 | 男女污污视频在线观看 | 四虎b7s22c0m| a亚洲视频| 久久性精品 | 亚洲在线观看一区 | 久青草国产观看在线视频 | 久久国产精品国语对白 | 四虎院影永久在线观看 | 亚洲精品久久久久久中文字幕小说 | 国产在线一区二区三区在线 | 国产精品伦理一区二区三区 | 国产成在线人视频免费视频 |