免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11071 | 回复: 5
打印 上一主题 下一主题

gettimeofday和clock_gettime的不同。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-31 10:46 |只看该作者 |倒序浏览
如题。谢谢。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-03-31 11:30 |只看该作者

gettimeofday和clock_gettime的不同。

clock_gettime
你在哪里看到的?

论坛徽章:
0
3 [报告]
发表于 2004-03-31 16:08 |只看该作者

gettimeofday和clock_gettime的不同。

在师兄编的一个程序里,好象是跟posix记时器有关,我们的系统是irix的。多指教,谢谢。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-03-31 17:48 |只看该作者

gettimeofday和clock_gettime的不同。

我的系统里面没这个函数。
你把这两个函数的man信息对比
着看一看吧。

论坛徽章:
0
5 [报告]
发表于 2007-12-21 16:54 |只看该作者
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

论坛徽章:
0
6 [报告]
发表于 2007-12-21 17:02 |只看该作者
AIX下面也得到同样的结果

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

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP