linux开发版最长问到的问题之一就是在linux下如何得到更精确的计时。其实有很多办法,比如以前就有人用select。不过现在有更精确的实时时钟可以用,这就是用CLOCK_PROCESS_CPUTIME_ID做参数通过timer_create来创建timer。通过clock_getres可以得到系统的精度。
实际上,上述方法底层用的就是CPU的rdtsc指令。 在linux下,可以用到CPU的rdtsc指令(当然,windows下也是这样。现在的CPU一般都支持这个指令)来得到一个CPU的时间戳(Time Stamp),这个值是每个指令周期都增加的,再根据CPU的频率就可以计算出时间来。用这种方式,对于频率高的CPU,甚至可以实现纳秒级的时间控制。不过据说因为精度太高,这种方式数据抖动比较厉害,每次结果都不一样,经常有几百甚至上千的差距。不过在我的实验中误差并没有这么大,上下在1%左右,不过如果你的要求高的话,这1%也是很厉害的。不过sleep调用也是会有误差的,这里就不去研究了。
下面是源码:
int get_rdtsc() ... {
asm("rdtsc");
}
int main() ... {
int i;
for(i=0;i<10;i++) ...{
int t1 = get_rdtsc();
sleep(1);
int t2 = get_rdtsc();
printf("t2 - t1 = %ld (%ldMHZ) ", t2 - t1, (t2-t1)/1000000);
}
}
我的实验环境:Intel E6320/2G,使用VMWare虚拟机,系统是centos5-x86。下面是输出结果。
t2 - t1 = 1865320163 (1865MHZ)
t2 - t1 = 1865996702 (1865MHZ)
t2 - t1 = 1862758710 (1862MHZ)
t2 - t1 = 1865247214 (1865MHZ)
t2 - t1 = 1863456686 (1863MHZ)
t2 - t1 = 1865427930 (1865MHZ)
t2 - t1 = 1865423429 (1865MHZ)
t2 - t1 = 1863616999 (1863MHZ)
t2 - t1 = 1865105795 (1865MHZ)
t2 - t1 = 1867224090 (1867MHZ)
2011-09-10 16:21:11
分类: C/C++
- #include<stdio.h>
- #include<stdlib.h>
- #include<linux/types.h>
- #define TIMES 100
- #define SIZE 1024
- __u64 rdtsc()
- {
- __u32 lo,hi;
- __asm__ __volatile__
- (
- "rdtsc":"=a"(lo),"=d"(hi)
- );
- return (__u64)hi<<32|lo;
- }
- int myfunction()
- {
- int i;
- char *p = NULL;
- for(i = 0;i<TIMES;i++)
- {
- p = (char*)malloc(SIZE*sizeof(char));
- if(p)
- {
- free(p);
- }
- else
- {
- printf("malloc failed when i = %d\n",i);
- }
- }
- return 0;
- }
- int test_rdtsc()
- {
- __u64 begin;
- __u64 end;
- begin = rdtsc();
- myfunction();
- end = rdtsc();
- printf("myfunction cost %llu CPU cycles\n",end-begin);
- return 0;
- }
- int main()
- {
- test_rdtsc();
- return 0;
- }
- root@libin:~/program/assembly/rdtsc# ./test
- myfunction cost 310949 CPU cycles