clock():捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick ,即“时钟打点”。

常数CLK_TCK:机器时钟每秒所走的时钟打点数。

 #include <stdio.h>
#include <time.h> colck_t start, stop;
/* clock_t 是clock() 函数返回的变量类型*/ double duration;
/* 记录被测函数运行时间,以秒为单位*/ int main()
{
/*不在测试范围内的准备工作写在clock()调用之前*/
start = clock(); /* 开始计时 */
MyFunction(); /* 把被测函数加在这里 */
stop = clock(); /* 停止计时 */
duration = ((double)(stop - start))/CLK_TCK; /* 其他不在测试范围的处理写在后面,eg:输出duration的值*/
}

实验_eg:执行下面打印一次“Hello World!”的时间

     注意:因为程序执行的太快,所以显示为0;

 #include <stdio.h>
#include <time.h>
void hello();
int main(){
clock_t start, stop;
double duration;
start = clock();
hello();
stop = clock();
duration = ((double)(stop - start))/CLK_TCK;
printf("该程序运行的时间是:%f\n",duration);
return ;
}
void hello(){
printf("Hello World!\n");
}

解决方案:让被测函数重复运行充分多次,使得测出的总的时钟打点间隔充分长,最后计算被测函数平均运行的时间。

 #include <stdio.h>
#include <time.h> #define MAXK 1e5 /* 被测函数最大重复调用次数 */ void hello();
int main(){
int i;
clock_t start, stop;
double duration;
start = clock();
for(i=; i<MAXK; i++){
hello();
}
stop = clock();
duration = ((double)(stop - start))/CLK_TCK/MAXK; printf("duration = %f\n",duration);//0.00003s左右
return ;
} void hello(){
printf("Hello world!");
}

C_使用clock()函数获取程序执行时间的更多相关文章

  1. 前端PHP入门-023-重点日期函数之程序执行时间检测

    我们有的时经常需要做程序的执行时间执行效率判断. 实现的思路如下: <?php //记录开始时间 //记录结整时 // 开始时间 减去(-) 结束时间 得到程序的运行时间 ?> 可是大家不 ...

  2. java获取程序执行时间

    第一种是以毫秒为单位计算的. //伪代码 long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 lon ...

  3. Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间

    1.问:知道程序执行时间我们能够做什么? 在<C++应用程序性能优化>一书中,假设大家读过相信大家一定对性能优化这一块很上心,文中总是对优化前后的时间对照很直观给我们一个感受. 那么我们怎 ...

  4. delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)

    uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...

  5. inux关于readlink函数获取运行路径的小程序

    inux关于readlink函数获取运行路径的小程序   相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int  re ...

  6. 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间

    https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ...

  7. 程序运行时间测试 - 使用libc 中 clock 函数

    我们运行程序的时候,可以简单使用clock函数测试程序的运行时间:(本示例中以微秒为单位输出) https://github.com/yaowenxu/Workplace/blob/master/ti ...

  8. 微信小程序云开发-云函数-数据库和云函数获取数据的区别

    一.数据库获取数据 1.1 数据库获取数据的写法 在本地创建的页面js文件中写代码 1.2 数据库获取数据返回数据限制20条 数据库获取数据,每次返回20条数据(数据库有108条数据) 1.3 数据库 ...

  9. 微信小程序云开发-云函数-调用初始云函数获取openid

    一.调用初始云函数获取openid的两种方法 1.传统的success和fail 2.ES6的.then和.catch 3.编译结果 说明:初始云函数,是指刚创建完成的云函数.默认系统写的代码.

随机推荐

  1. admin 后台操作表格

    1. app下创建 templates  运行的时候 先找全局的templates——> 按照app的注册顺序找templates中的文件 2. app下在创建一个urls.py include ...

  2. JavaScript 基本类型和引用类型

    前言 ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 基本类型 Undefined.Null.B ...

  3. SpringBoot系列: 所有配置属性和官方文档

    Spring Boot 通用配置参数https://docs.spring.io/spring-boot/docs/current/reference/html/common-application- ...

  4. [物理学与PDEs]第1章第2节 预备知识 2.3 Faraday 电磁感应定律

    1.  Faraday 电磁感应定律: 设 $l$ 为任一闭曲线, 则 $$\bex \oint_l{\bf E}\cdot\rd {\bf l} =-\int_S \cfrac{\p {\bf B} ...

  5. Win7 x64位打开VirtualBox报错处理。

    错误代码如下: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown work ...

  6. html(jQuery)替换字符串(全部替换)

    var  str= "a<br/>b<br/>c<br/>"; var Newstr = str.replace("<br/&g ...

  7. PostgreSQL快速入门

    一.PostgreSQL是什么? PostgreSQL是一个功能强大的开源对象关系数据库管理系统(ORDBMS). 用于安全地存储数据; 支持最佳做法,并允许在处理请求时检索它们. PostgreSQ ...

  8. 【原创】大数据基础之Airflow(1)简介、安装、使用

    airflow 1.10.0 官方:http://airflow.apache.org/ 一 简介 Airflow is a platform to programmatically author, ...

  9. 【原创】大叔案例分享(4)定位分析--见证scala的强大

    一 场景分析 定位分析广泛应用,比如室外基站定位,室内蓝牙beacon定位,室内wifi探针定位等,实现方式是三点定位 Trilateration 理想情况 这种理想情况要求3个基站‘同时’采集‘准确 ...

  10. 【原创】大叔经验分享(45)kibana添加index pattern卡住 返回403 Forbidden

    kibana添加index pattern卡住,通过浏览器查看请求返回状态为403 Forbidden,返回消息为: {"message":"blocked by: [F ...