在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
相关文档:
总览
用iptables -ADC 来指定链的规
则
,-A添加 -D删除 -C 修改
iptables - [RI] chain rule num rule-specification[option]
用iptables - RI 通过规则的顺序指定
iptables -D chain rule num[option]
删除指定规则
iptables -[LFZ] [chain][option]
用iptables -LFZ 链名 [选项]
iptables -[NX] chain
用 -NX ......
whereis 命令详解
功能说明:查找文件。
语 法:whereis [-bfmsu][-B ...][-M ...][-S ...][文件...]
补充说明:whereis指令会在特定目录中查找符合条件的文件。这些文件的烈性应属于原始代码,二进制文件,或是帮助文件。
参 数:
-b 只查找二进制文件。
-B 只在设置的目录下查找二进制文件。
-f ......
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 ......
线程的同步, 发生在多个线程共享相同内存的时候, 这时要保证每个线程在每个时刻看到的共享数据是一致的. 如果每个线程使用的变量都是其他线程不会使用的(read & write), 或者变量是只读的, 就不存在一致性问题. 但是, 如果两个或两个以上的线程可以read/write一个变量时, 就需要对线程进行同步, 以确保它们在访问该变 ......
公司的服务器是CentOS 5.4(Final)版的,今天在上面安装了一回mysql5.5,记录如下:
1、从http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.2-m2.tar.gz 下载源码安装包到/usr/local/src
2、tar zxvf mysql-5.5.2-m2.tar.gz 解压到/usr/local/src/mysql-5.5.2-m2下
cd mysql-5.5.2-m2/
&n ......