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)可获得所运行系统每秒的滴答数。
typedef long clock_t;
- //windows
- #include <stdio.h>
- #include <time.h>
- int main()
- {
- printf("%ld\n", CLOCKS_PER_SEC);
- return 0;
- }
输出结果为:1000
- //Ubuntu
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/times.h>
- int main(int argc, char* argv[])
- {
- int sec_per_dida = sysconf( _SC_CLK_TCK);
- printf("%d\n", sec_per_dida);
- return 0;
- }
输出结果为:100
time的例子:
- #include <stdio.h>
- #include <windows.h>
- #include <time.h>
- int main()
- {
- long beg;
- long end;
- beg = time(0);
- Sleep(5000);
- end = time(0);
- printf("%ld\n", end - beg);
- return 0;
- }
输出结果为5,可知time返回的单位是滴答。