Clock()函数简单使用(转)

存在于标准库<time.h>

描述

C 库函数 clock_t clock(void) 返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。为了获取 CPU 所使用的秒数,您需要除以 CLOCKS_PER_SEC。

在 32 位系统中,CLOCKS_PER_SEC 等于 1000000,该函数大约每 72 分钟会返回相同的值。

声明

下面是 clock() 函数的声明。

clock_t clock(void)

参数

  • NA

返回值

该函数返回自程序启动起,处理器时钟所使用的时间。如果失败,则返回 -1 值。

实例

下面的实例演示了 clock() 函数的用法。

实例

#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); }

让我们编译并运行上面的程序,这将产生以下结果:

程序启动,start_t = 2614
开始一个大循环,start_t = 2614
大循环结束,end_t = 28021
CPU 占用的总时间:0.025407
程序退出...
原文链接:https://www.runoob.com/cprogramming/c-function-clock.html

最新文章

  1. recyleView使用笔记
  2. 第一节(配置springmvc环境)学习尚硅谷-springmvc视频教程
  3. Windows 安装启动apache时出现错误的解决方法
  4. Silverlight 动画详解
  5. windows 7 安装 telnet
  6. Django自定义模型(model)中的字段标签
  7. .frm文件
  8. Spring Boot快速入门(三):依赖注入
  9. UE4 UnLoadStreamLevel
  10. iOS页面切换动画实现方式。
  11. Android光线传感器-android学习之旅(65)
  12. Struts2框架原理
  13. 树形动态规划(树状DP)小结
  14. three.js 制作一个简单的圆柱体模型
  15. windows10 设置软件开机启动
  16. 20170831 php
  17. (转)远程连接webservice遇到无法访问的问题解决办法
  18. 利用word的VBA,为代码统一表格宽度,底色及行号
  19. zookeeper原理及功能介绍(转)
  20. dfs序题目练习

热门文章

  1. PHP restore_exception_handler() 函数
  2. Servlet容器启动过程
  3. SSH整合-hibernate
  4. SpringMvc响应数据和结果视图
  5. 基于视频压缩的实时监控系统-sprint2采集端图像采集子系统设计
  6. 【学习笔记】ThreadLocal与引用类型相关知识点
  7. 【系统之音】WindowManager工作机制详解
  8. jQuery 综合练习ToDoList
  9. 曲线生成与求交—B样条曲线
  10. css 命名规则 BEM!