欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

likely()與unlikely()

系統(tǒng) 2137 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 中判斷語句經(jīng)常會(huì)看到likely和unlikely,例如:

      
        if(likely(
        
          value)){
        
      
    
      }
    
      
        else{
      
    
      }
    
      ?
    

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

也就是likely和unlikely是一樣的,但是實(shí)際上執(zhí)行是不同的,加likely的意思是value的值為真的可能性更大一些,那么執(zhí)行if的機(jī)會(huì)大,而unlikely表示value的值為假的可能性大一些,執(zhí)行else機(jī)會(huì)大一些。 加上這種修飾,編譯成二進(jìn)制代碼時(shí)likely使得if后面的執(zhí)行語句緊跟著前面的程序,unlikely使得else后面的語句緊跟著前面的程序,這樣就會(huì)被cache預(yù)讀取,增加程序的執(zhí)行速度,likely和unlikely的實(shí)現(xiàn)在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的一個(gè)預(yù)處理命令,其解釋如下:

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.

轉(zhuǎn)自:http://www.cnblogs.com/yangzd/archive/2010/09/27/1837202.html

likely()與unlikely()


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲欧美精品一区二区 | 国产成人综合网 | 色婷亚洲| 欧美日韩一区二区三区在线观看 | 欧美伦理一区 | 日韩综合区 | 激情五月色婷婷 | 日本一区二区三区中文字幕 | 国产精品综合色区在线观看 | 亚洲国产一区在线 | 日韩欧美不卡 | 奇米影视四色中文字幕 | 亚洲精品色综合久久 | 国产精品资源在线观看网站 | 国产九九视频在线观看 | 九九操视频 | 一区二区三区国产精品 | 精品久久一区 | 91se在线看片国产免费观看 | 一级毛片视频免费 | 免费精品一区二区三区在线观看 | 亚洲欧美国产一区二区三区 | 日本高清视频在线播放 | 九九久久精品视频 | 九九这里只有精品视频 | 日本捏胸摸下面免费视频 | 看片一区| 日本一区二区三区免费观看 | 三级国产视频 | 国产精品拍拍拍福利在线观看 | 日本午夜在线 | 成人年鲁鲁在线观看视频 | 综合二区| 久久久国产精品x99av | 国产高清在线观看 | 免费九九视频 | 欧美激情久久欧美激情 | 精品入口麻豆 | 亚洲影院在线观看 | 久久99热这里只频精品6中文字幕 | 亚洲福利影院 |