likely()与unlikely()
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
随机推荐
- SpringMVC源码解析- HandlerAdapter - ModelFactory(转)
ModelFactory主要是两个职责: 1. 初始化model 2. 处理器执行后将modle中相应参数设置到SessionAttributes中 我们来看看具体的处理逻辑(直接充当分析目录): 1 ...
- Objective-C语言的一些基础特性
OC与C++.Java等面向对象语言有很多的类似之处,不过在很多方面也是有所差别的.若是用过某一种面向对象语言,那么就很容易理解OC语言所用的范式和模板了.但是在语法使用上,也许会显得陌生.因为OC语 ...
- JMS分布式应用程序异步消息解决方案EhCache
高速缓存同步问题
部分博客中描述的使用拦截器怎么用EJB公布的WebService加入缓存,这样能够提高WebService的响应效率.但是即使是这样做,还是要经历网络的传输的.于是决定在调用WebService的程序 ...
- Install Orace 11g on Solaris 10 Sparc 64 bit
昨天有一个客户端安装11g数据库.整个安装过程和一些遇到的问题是一个创纪录.共享. 由于客户不能使用自己的机器远程连接到server,意通过U盘.移动硬盘等拷贝不论什么文件.因此一些记录内容无法做到非 ...
- 付款页面DEMO
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHRxdWVlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- 国内Android应用推广的六大主流方式
国内Android应用推广的六大主流方式 http://mobi.baike.com/article-19433.html 随着Android市场份额的飞速增长,越来越多的国内开发团队和公司開始投入A ...
- 如何设置eclipse在默认模式下打开文件
如何设置eclipse在默认模式下打开文件 打开eclipse.选择例如以下:windows --> preferences --> General --> Editors --&g ...
- Laravel nginx 伪静态规则
最近的各种调查PHP相框(CI, Cake, ThinkPHP, Laravel, Yii)情绪Laravel它看起来很漂亮,下一个深入了解.用发展机Apache,Stage在运行nginx,一旦部署 ...
- 编程获取linuxservercpu、内存和磁盘使用
proc文件系统简介 /proc文件系统是一个伪文件系统.它是唯一的,其中存储器,如果不采取外部存储空间. 它是文件系统提供了与内核进程进行通信的接口的方法.用程序能够通过/proc得到系统的信息.并 ...
- SAP ABAP规划 使用LOOP READ TABLE该方法取代双LOOP内部表的方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlueXVlemhhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...