gettimeofday

gettimeofday


reference
manps.还有介绍settimeofday()
利用gettimeofday计算程序运行时间--精确到毫秒ps.有代码

futher reading
32位与64位下各类型长度对比ps.主要在于long和指针,还有64位系统下编程需要注意的几个问题。


The functions gettimeofday and settimeofday can get and set the time as
well as a timezone.

#include <sys/time.h>
       int gettimeofday(struct timeval *tv, struct timezone *tz);

"tv"

The tv argument is a timeval struct, as specified in

struct timeval {
               time_t      tv_sec;     /* seconds秒 */
               suseconds_t tv_usec;    /* microseconds毫秒 */
           };
  • gives the number of seconds and microseconds since the
    Epoch:1970-01-01 00:00:00 +0000 (UTC). Traditionally, the fields of
  • struct timeval were of type long.

return value

gettimeofday() and settimeofday() return 0 for success, or -1 for
failure (in which case errno is set appropriately).

examples

1.ms

#include <sys/time.h>  
#include <<font color="#ff0000">sys/time.h>
long getCurrentTime()  
{  
   struct timeval tv;  
   gettimeofday(&tv,NULL);  
   return tv.tv_sec * 1000 + tv.tv_usec / 1000;  
}  
  
int main()  
{  
    long start = getCurrentTime();
    printf("c/c++ program:%ld\n",getCurrentTime()); 
    //设置延迟
    for (int i = 0; i<100000; ++i)
    {  ;  }
    printf("The program has run %ld ms", getCurrentTime()-start);
    return 0;  
}

转载于:https://www.cnblogs.com/dunfentiao/p/4323439.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值