clock time times

1. clock,windows下的time一样, 也和 linux下的times一样

2. clock返回clock_t型,其实就是长整型

3. clock和times返回的单位是滴答

4. 滴答根据不同的系统,和不同的标准而不同


    The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To convert the return value to seconds, divide it by CLOCKS_PER_SEC.(windows)


    times 和 clock都收用来计算cpu时间。times函数的返回值是进程迄今为止的存活时间。所有时间都是以“滴答”为单位的,linux系统下,函数 sysconf(_SC_CLK_TCK)可获得所运行系统每秒的滴答数。


clock和time的返回值都是long型

typedef long clock_t;


  1. //windows 
  2. #include <stdio.h> 
  3. #include <time.h> 
  4.  
  5.  
  6. int main() 
  7.     printf("%ld\n", CLOCKS_PER_SEC); 
  8.     return 0; 

输出结果为:1000

  1. //Ubuntu 
  2. #include <stdio.h> 
  3. #include <unistd.h> 
  4. #include <sys/times.h> 
  5.  
  6. int main(int argc, char* argv[]) 
  7.         int sec_per_dida = sysconf( _SC_CLK_TCK); 
  8.         printf("%d\n", sec_per_dida); 
  9.     return 0; 

输出结果为:100

time的例子:

  1. #include <stdio.h> 
  2. #include <windows.h> 
  3. #include <time.h> 
  4.  
  5. int main() 
  6.     long beg; 
  7.     long end; 
  8.     beg = time(0); 
  9.     Sleep(5000); 
  10.     end = time(0); 
  11.     printf("%ld\n", end - beg); 
  12.      
  13.     return 0; 

输出结果为5,可知time返回的单位是滴答。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值