clock time times

来源:互联网 发布:神仙劫宠物进阶数据 编辑:程序博客网 时间:2024/05/16 06:30

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;


[cpp] view plaincopyprint?
  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

[cpp] view plaincopyprint?
  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的例子:

[cpp] view plaincopyprint?
  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返回的单位是滴答。

原创粉丝点击