在linux中的likely和unlikely
在linux中的likely和unlikely
0
推荐
在linux中判断语句经常会看到likely和unlikely,例如:
if(likely(value)){
}
else{
}
简单从表面上看if(likely(value)) == if(value),if(unlikely(value)) == if(value)。
这两个宏对程序运行结果没有影响,只是用于提高程序效率。其实现和gcc编译器密切相关。
具体点说,就是如果你觉得程序运行时候一般sndcmd|=0的可能性比较大,那么就加上likely的macro;反之则加unlikely。
也就是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 i
相关文档:
由于 Linux 良好的用户权限管理体系,病毒往往是 Linux 系统管理员最后才需要考虑的问题。以往,Linux 上的杀毒软件主要是为企业的邮件和文件服务器所设计的。如今,随着 Linux 桌面用户数量的增长,桌面用户在受益于 Linux 系统对病毒较强的天然免疫力的同时,也需要杀毒软件清理从网络或U盘带来的WIndows病毒。尽管那些 ......
[url=http://www.netdigedu.com/jiagoubaike/13063.html]Linux:Linux文件命令精通指南[/url]
[url=http://www.netdigedu.com]网讯通信学院[/url]
为刚接触 Linux 文件命令的初学者提供的速成教程
虽然 GUI 桌面(如 KDE 和 GNOME)能够帮助用户利用 Linux 特性,而无需关于命令行接口的功能知识,但还是经常会需要更多的 ......
=======================================================================
一些关于Linux的资源站点,希望对大家有帮助
http://www.linux.org/ ;
Linux官方新闻和信息网站。
http://freesoft.cei.gov.cn/ ;
中国软件行业协会国际自由软件应用研究发展分会的自由软件库,上面提供有各种Linux软件可供下载,并有Linux ......
Linux下读硬盘序列号的程序
/*
* gethddsn.c
*
* Get serial number of ide hard disk.
* example: my Maxtor 15G 's s/n is K306S04C.
*
* Compile with: gcc -O2 gethddsn.c
*
* Xiaoming DONG <xmdong@263.net>
* Aug 24, 2000
*
*/
#include <stdio.h>
#include <stdlib.h>
#includ ......
1.安装JDK
首先,下载最新版本的Linux 平台的JDK,建议下载RPM自解压格式的例如本文所用jdk-1_5_0_06-linux-i586-rpm.bin,先下载文件到/tmp,打开终端,输入:
cd /tmp
su
输入root密码
直接执行文件:
./jdk-1_5_0_06-linux-i586-rpm.bin
然后会出现sun的协议(Sun Microsystems, Inc. Binary Code License Agreeme ......