热点推荐:ASP.Net | ADO.Net | VB.Net | Web服务器 | Access | MSSQL | MySQL | Oracle | .Net控件 | Win 9x | Win 2000 | Win 2003 | DOS | Unix | 注册表 | 应用其它 | 安装调试 | 基本操作 | 使用技巧 | 系统优化 |故障处理 | 个性风格 | 病毒安全 | 专杀工具
您现在的位置: 中华IT技术网 >> FAQ >> C/C++ >> C语言 >> 正文
全文
C中获取当前时间的函数
作者:1024k    文章来源:本站原创    更新时间:2007-9-22

        突然要用到C程序里调用当前时间,来测试一段代码的运行时间。找了一下是否有可以调用的库函数,没想到真的有:gettimeofday。因为这里的这个应用蛮常用的,所以留个记录在此。

  #include <stdio.h>
  #include <stdlib.h>
 
  struct timeval
  {
      long tv_sec; /* 秒数 */
      long tv_usec; /* 微秒数 */
  };
 
 struct timezone
 {
     int tv_minuteswest;
     int tv_dsttime;
 };
 
 int gettimeofday(struct timeval *tv,struct timezone *tz);
 
 void function()
 {
    // function to run some time consuming codes
 }
 
 int main(int argc, char *argv[])
 {
     struct timeval tpstart,tpend;
         float timespend;
 
     gettimeofday(&tpstart,NULL);
     function();
     gettimeofday(&tpend,NULL);
         timespend = tpend.tv_sec - tpstart.tv_sec;
         printf("Time Spend: %d", timespend);
     return EXIT_SUCCESS;
 }

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 相关文章
    最新更新
    编辑推荐
    热门图片
    频道大全
    文章阅读排行
    周排行
    月排行