gettimeofday和clock_gettime的不同

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


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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值