likely()与unlikely()

时间:2022-12-12 19:06:57

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

随机推荐

  1. Partition2:对表分区

    在SQL Server中,普通表可以转化为分区表,而分区表不能转化为普通表,普通表转化成分区表的过程是不可逆的,将普通表转化为分区表的方法是: 在分区架构(Partition Scheme)上创建聚集 ...

  2. Android系统build&period;prop文件

    # begin build properties (开始设置系统性能) # autogenerated by buildinfo.sh (通过设置形成系统信息) ro.build.id=GRI40 ( ...

  3. Spark&plus;Hadoop&plus;Hive集群上数据操作记录

    [rc@vq18ptkh01 ~]$ hadoop fs -ls / drwxr-xr-x+ - jc_rc supergroup 0 2016-11-03 11:46 /dt [rc@vq18ptk ...

  4. MySQL数据丢失情况分析

    一.存储引擎层面丢失数据                                                       由于在实际项目中,我们往往使用支持事务的InnoDB存储引擎.我们 ...

  5. openfire过滤脏话插件,控制消息是否发送

    参考:http://myopenfire.com/article/getarticle/9 package com.myopenfire.plugin; import java.io.File; im ...

  6. Android手机SSH Client客户端推荐JuiceSSH

    Windows上建立ssh服务器 参见: http://www.cnblogs.com/xred/archive/2012/04/21/2461627.html Android手机SSH Client ...

  7. wpf阻止键盘快捷键alt&plus;space,alt&plus;F4

    /// <summary>        /// 阻止 alt+f4和alt+space 按键        /// </summary>        /// <par ...

  8. Mysql必须知道的知识

    最近在准备面试,所以也整理了一些Mysql数据库常用的知识,供大家参考. 1.MySQL的复制原理以及流程 (1).复制基本原理流程 1. 主:binlog线程--记录下所有改变了数据库数据的语句,放 ...

  9. aways on 配置部署(一)——准备工作

    sqlserver的aways on 配置需要经历三个步骤,前面两个步骤是对aways on 配置的一个准备步骤. 经过了一个星期的研究,终于成功的完成了前两个步骤,期间参考了很多的资料和博客,总感觉 ...

  10. 全国天气预报信息数据 API 功能简介与代码调用实战视频

    此文章对开放数据接口 API 之「全国天气预报信息数据 API」进行了功能介绍.使用场景介绍以及调用方法的说明,供用户在使用数据接口时参考之用,并对实战开发进行了视频演示. 1. 产品功能 接口开放了 ...