• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
honghonging
博客园    首页    新随笔    联系   管理    订阅  订阅

c/c++求时间差

clock_t clock(),clock()

    获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到1/CLOCKS_PER_SEC秒。

    测试程序如下:

[c-sharp]  view plain copy
  1. #include <time.h>  
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.     double start,end,cost;  
  6.     start=clock();  
  7.     sleep(1);  
  8.     end=clock();  
  9.     cost=end-start;  
  10.     printf("%f/n",cost);  
  11.     return 0;  
  12. }  
 

获取时间用time_t time( time_t * timer ),计算计算机休眠时间差使用double difftime( time_t timer1, time_t timer0 )。 精确到秒。

测试程序如下:

[c-sharp]  view plain copy
  1. #include <time.h>  
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.     time_t start ,end ;  
  6.     double cost;  
  7.     time(&start);  
  8.     sleep(1);  
  9.     time(&end);  
  10.     cost=difftime(end,start);  
  11.     printf("%f/n",cost);  
  12.     return 0;  
  13. }  
 
posted @ 2022-08-15 10:48  偶轰轰  阅读(465)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3