if-else 分支预测
来源:互联网 发布:软件类上市公司 编辑:程序博客网 时间:2024/05/01 00:30
关于if-else分支预测Branch_predictor的分析
- #ifdef __GNUC__
- #define LIKELY(x) (__builtin_expect(!!(x), 1))
- #define UNLIKELY(x) (__builtin_expect(!!(x), 0))
- #else
- #define LIKELY(x) (x)
- #define UNLIKELY(x) (x)
- #endif
#ifdef __GNUC__
有时候,我们在看linux的源码时,会看到如下类似的代码:
- if (UNLIKELY(fd < 0))
- {
- /* Do something */
- }
- else
- {
- /* Do other something */
- }
if (UNLIKELY(fd < 0)){ /* Do something */}else{ /* Do other something */}
- if (LIKELY(!err))
- {
- /* Do something */
- }
- else
- {
- /* Do other something */
- }
if (LIKELY(!err)){ /* Do something */}else{ /* Do other something */}
LIKELY 作用是告诉编译器 x表达式更有可能是真的,也就是有很大的几率是运行if分支的。
UNLIKELY 作用是告诉编译器 x表达式更有可能是假的,也就是有很大的几率是运行else分支的。
这样的好处是如果if-else分支预测正确时,可以大大提高程序运行的效率。
参考网址:https://stackoverflow.com/questions/109710/likely-unlikely-macros-in-the-linux-kernel-how-do-they-work-whats-their
阅读全文
0 0
- if-else 分支预测
- if/else 分支控制语句
- 应用分支结构if...else....
- if...else if switch分支结构
- if---else---if分支的执行次序。
- Python中的条件分支语句,if , else , else if
- mips汇编语言实现if-else分支结构
- 为什么使用if else分支语句不好
- 过多if-else分支的优化
- 过多if-else分支的优化
- 汇编的if else的分支结构
- 过多if-else分支的优化
- 分支结构 if...else和switch...case
- swift 2.0 分支语句 if...else & switch
- Java 分支结构 - if...else/switch
- Java 分支结构 - if...else/switch
- 过多if-else分支的优化
- 过长的if-else分支结构优化
- dubbo插件机制
- click事件多次触发 jquery
- seo视频教程:网站目标关键词选择和设置
- JDBC连接数据库基础
- ASP.NET Config读写示例
- if-else 分支预测
- abstract类及其方法
- SolidWorks 部分插件不显示
- C++ Operator Precedence c++运算符优先级列表
- jquery实现的返回顶部+侧边栏功能
- mysql重连,连接丢失
- form下载文件,指向iframe ,如何判断iframe加载完成 form.submit之后的事件问题 监听form表单下载文件是否完成
- Android数据加密之Base64编码算法
- js中replace全部替换的正则表达式