likely(x)和unlikely(x)

简介: #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) __builtin_expect() 是 GCC (version >= 2.96)提供给程序员使用的,目的是将“分支转移”的信息提供给编译器,这样编译器可以对代码进行优化,以减少指令跳转带来的性能下降。

#define likely(x) __builtin_expect((x),1)

#define unlikely(x) __builtin_expect((x),0)

__builtin_expect() GCC (version >= 2.96)提供给程序员使用的,目的是将分支转移的信息提供给编译器,这样编译器可以对代码进行优化,以减少指令跳转带来的性能下降。

__builtin_expect((x),1) 表示 x 的值为真的可能性更大;__builtin_expect((x),0) 表示 x 的值为假的可能性更大。

附:

long __builtin_expect (long exp, long c)          [Built-in Function]

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.

原文:

[1]http://wenku.baidu.com/view/dae3a5eb172ded630b1cb621.html

[2]http://linux.chinaunix.net/techdoc/develop/2007/08/31/966838.shtml

[3]http://hi.baidu.com/lxk3480/item/91eae1c6c1690124a0b50ae5

[4]http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

目录
相关文章
|
11月前
|
NoSQL 测试技术 Linux
VLDB顶会论文Async-fork解读与Redis在得物的实践(3)
VLDB顶会论文Async-fork解读与Redis在得物的实践
137 0
VLDB顶会论文Async-fork解读与Redis在得物的实践(3)
|
NoSQL Redis Anolis
性能优化特性之:Redis批处理pipeline模式
本文介绍了一种更贴近实际使用的redis验测方法:多pipline模式,并从原理、使用方法进行详细阐述。
|
弹性计算 Kubernetes Serverless
技术干货:解密最受欢迎的开源 Serverless 框架弹性技术实现
技术干货:解密最受欢迎的开源 Serverless 框架弹性技术实现
|
缓存 Linux C语言
|
开发工具 git C++
【Git】stash 仅贮存指定文件的修改
如何使用 git stash 贮存单个或多个文件
2670 0
|
XML Java Android开发
修改Android设备型号、版本号、去掉自定义版本显示
修改Android设备型号、版本号、去掉自定义版本显示
1039 0
|
存储 缓存 大数据
对象存储上如何重复利用高速缓存和算子下推(一)
 1 背景基于snowflake,redshift等在云上数仓的开创性工作,基于对象存储构建数据湖/数仓已经成为一股新的潮流,现在的云上数据库通常都采用计算-存储分离的架构,而不再是传统的share-nothing,这是由对象存储的的高弹性,低成本带来的优势,但是有与对象的存储特性,其在单流性能方面的弱点也很明显。目前的常见的场景中,如离线分析,基本都只能采用大规模并发的方式大量调用对象存储的AP
对象存储上如何重复利用高速缓存和算子下推(一)
|
缓存 Java Go
Go mod包依赖管理工具使用详解
Go mod包依赖管理工具使用详解
469 0
Go mod包依赖管理工具使用详解
|
存储 弹性计算 运维
Serverless 架构落地实践及案例解析
从单体架构到微服务架构,从单机部署到集群化部署,互联网软件架构越来越复杂,公司需要投入大量精力和成本进行底层技术的升级和维护。但是 Serverless 架构能够让开发者专注业务实现从而创造更大的业务价值。
|
NoSQL Redis 数据安全/隐私保护
【Docker】6、Docker搭建Redis高可用Cluster集群环境
本篇文章将使用 docker 搭建 Redis 高可用 Cluster 集群环境,我们采用三主三从模式,使用 6 个节点搭建 Cluster 集群环境
654 0