小白教程
所有教程
关于
Search
172.71.254.67
172.71.254.67
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“C function clock”的源代码
本页内容
上一节:
C_function_localtime
下一节:
C_function_asctime
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:clock_t clock(void);}}[[Category:cpp date time|4]] = C 库函数 - clock() = [[c standard library time h|C 标准库 - <time.h>]] == 描述 == C 库函数 '''clock_t clock(void)''' 返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。为了获取 CPU 所使用的秒数,您需要除以 CLOCKS_PER_SEC。 在 32 位系统中,CLOCKS_PER_SEC 等于 1000000,该函数大约每 72 分钟会返回相同的值。 == 声明 == 下面是 clock() 函数的声明。 <pre> clock_t clock(void) </pre> == 参数 == * '''NA''' == 返回值 == 该函数返回自程序启动起,处理器时钟所使用的时间。如果失败,则返回 -1 值。 下面的实例演示了 clock() 函数的用法。 <sample title="" desc="" lang="c" hererun="1"> #include <time.h> #include <stdio.h> int main() { clock_t start_t, end_t; double total_t; int i; start_t = clock(); printf("程序启动,start_t = %ld\n", start_t); printf("开始一个大循环,start_t = %ld\n", start_t); for(i=0; i< 10000000; i++) { } end_t = clock(); printf("大循环结束,end_t = %ld\n", end_t); total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; printf("CPU 占用的总时间:%f\n", total_t ); printf("程序退出...\n"); return(0); } </sample> 让我们编译并运行上面的程序,这将产生以下结果: <pre> 程序启动,start_t = 2614 开始一个大循环,start_t = 2614 大循环结束,end_t = 28021 CPU 占用的总时间:0.025407 程序退出... </pre> [[c standard library time h|C 标准库 - <time.h>]]
返回至“
C function clock
”。
上一节:
C_function_localtime
下一节:
C_function_asctime