LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 803|回复: 6

请问一个关于系统时间的问题!

[复制链接]
发表于 2005-4-21 09:43:21 | 显示全部楼层 |阅读模式
我现在想计算我的一个程序的运行时间,在windows下我用计算后的时间减去计算前的时间就可以了,在linux我不知道怎么去抓系统时间,请问各位该怎么做?
多谢 :help
发表于 2005-4-21 10:01:48 | 显示全部楼层
time grep
will tell you the exec time of the command "grep"
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-21 10:13:15 | 显示全部楼层
请问在c++里怎么抓系统时间,需要哪个头文件?可以给个例子吗?多谢了!!!
回复 支持 反对

使用道具 举报

发表于 2005-4-21 10:25:36 | 显示全部楼层
#include <time.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
   time_t tt_begin, tt_finish;

   tt_begin = time(NULL);  /* mark the start time */

  // your code    ...............


   tt_finish = time(NULL); /* mark the time again */

   printf("The difference is: %f seconds\n",difftime(tt_begin,tt_finish));

   return 0;
}

More APIs can be found at
#man 2 time
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-21 11:00:25 | 显示全部楼层
Thank you !!!!!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-21 14:48:56 | 显示全部楼层
请问还有没有更精确一点的方法,这个方法只能精确到秒,可以精确到微秒吗?
多谢!
回复 支持 反对

使用道具 举报

发表于 2005-4-21 15:36:18 | 显示全部楼层
gettimeofday can help you in this case.
use "man gettimeofday" to get more help.
Linux is not a realtime-OS, so accuracy of the measure will be relatively low. This may not reflect the reality.
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表