http://www.9php.com/FAQ/cxsjl/c/2007/12/0405444108599.html

2007-12-21 16:54ovipgdft

clock_gettime比gettimeofday更加精确
简单做了一下测试
#include<time.h>
#include<stdio.h>
#define MILLION 1000000
int main(void)
{
        struct timespec tpstart;
        struct timespec tpend;
        long timedif;
        clock_gettime(CLOCK_MONOTONIC, &tpstart);
        clock_gettime(CLOCK_MONOTONIC, &tpend);
        timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
        fprintf(stdout, “it took %ld microseconds\n”, timedif);
        return 0;
}
在linux 2.6内核下面
gcc -o test test.c -lrt
./test
得到结果:
it took 2 microseconds
#include<time.h>
#include<stdio.h>
#define MILLION 1000000
int main(void)
{
        struct timespec tpstart;
        struct timespec tpend;
        long timedif;
        gettimeofday(&tpstart, NULL);
         gettimeofday(&tpend, NULL);
        timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
        fprintf(stdout, “it took %ld microseconds\n”, timedif);
        return 0;
}
gcc -o test test.c
./test
得到结果:
it took 0 microseconds

2007-12-21 17:02ovipgdft

AIX下面也得到同样的结果
而且可以通过clock_gettime这个函数测试出64位程序要比32为程序运行快
在AIX P570,16个CPU 15.5G内存机器上测试了一把
用64位模式得到的结果是 0 microseconds
用32位模式得到的结果是 1 microsecond

posted on
2011-03-21 10:06 
海王 
阅读(10090
评论(0
编辑 
收藏 
举报

版权声明:本文为leaven原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/leaven/archive/2011/03/21/1989946.html