C/C++下测量函数运行时间

time.h介绍

C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。

clock_t clock( void );

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif

很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000) //CLOCKS_PER_SEC为系统自定义的

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。

写法

#include<stdio.h>
#include<time.h> int main()
{
clock_t cBeg=clock();
//···
//调用函数
//···
clock_t cEnd=clock();
printf("program exection time: %.3f\n",(double)(cEnd-cBeg)/CLOCKS_PER_SEC);
return 0;
}

例子

#include<stdio.h>
#include<time.h> //直接递归
long long fib1(int n)
{
if(n==1||n==0)
return 1;
else
return fib1(n-1)+fib1(n-2);
} //尾递归
long long fib2(int n,long long f,long long s)
{
if(n<2)
return s;
else
return fib2(n-1,s,f+s);
} //迭代
long long fib3(int n)
{
long long f=1;
long long g=0;
while(n--)
{
f=f+g;
g=f-g;
}
return f;
} int main()
{
clock_t tBeg,tEnd;
tBeg=clock();
printf("%lld\n",fib1(40));
tEnd=clock();
printf("fib1 execution time: %.3f s\n",(double)(tEnd-tBeg)/CLOCKS_PER_SEC); tBeg=clock();
printf("%lld\n",fib2(40,1,1));
tEnd=clock();
printf("fib1 execution time: %.3f s\n",(double)(tEnd-tBeg)/CLOCKS_PER_SEC); tBeg=clock();
printf("%lld\n",fib3(40));
tEnd=clock();
printf("fib1 execution time: %.3f s\n",(double)(tEnd-tBeg)/CLOCKS_PER_SEC);
}

执行结果如下:

(尾递归由于编译器优化和迭代比直接递归快很多)

另外,linux下可直接 time ./执行程序测试时间。

C/C++下测量函数运行时间的更多相关文章

  1. python测量函数运行时间长度

    python测试函数运行时间长度的方法如下 import time def measure_time(): def wraps(func): def mesure(*args,**kwargs): s ...

  2. linux下统计程序/函数运行时间(转)

    一. 使用time 命令 例如编译一个hello.c文件 #gcc hello.c -o hello 生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令 #time ./hello ...

  3. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数009,Measure,测量函数

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数009,Measure,测量函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替 ...

  4. 用SWD调试接口测量代码运行时间 ( SWO )

    用SWD调试接口测量代码运行时间 关于时间测量的种种问题 在嵌入式中,我们经常需要测量某段代码的执行时间或测量事件触发的时间,常规的思路是: 1:在测量起始点,反转电平2:在测量结束点,再次反转电平 ...

  5. [Android Memory] Linux下malloc函数和OOM Killer

    http://www.linuxidc.com/Linux/2010-09/28364.htm Linux下malloc函数主要用来在用户空间从heap申请内存,申请成功返回指向所分配内存的指针,申请 ...

  6. linux和window下mkdir函数问题(转-锦曦月)

    通过WIN32宏进行判断   window下mkdir函数   #include<direct.h> int _mkdir( const char *dirname );   linux下 ...

  7. linux下syscall函数,SYS_gettid,SYS_tgkill

    出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html     linux下syscall函数,SYS_gettid,SYS_tgkill  ...

  8. hdwiki model目录下的函数类

    model目录下的函数类    actions.class.php(站内地图相关) getHTML:获得页面菜单和相关信息 getMap:生成站内地图 adv.class.php 对wiki_adve ...

  9. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

随机推荐

  1. Android sqlite 使用框架

    Android数据库存储 前言: 今天无聊就试试水,写写博客,在之前andorid使用数据储存好像大概有5种方式,其中轻量级的是sqlite数据库,但是sqlite使用起来好像有麻烦,首先要判断…之后 ...

  2. 数据链路层--PPP协议

    数据链路层使用的信道主要有两种类型:点对点信道和广播信道. 点对点 路由器在转发分组时只使用了下面的三层. 链路是从一个结点到相邻结点的一段物理线路,中间没有其他交换结点. 必须有一些必要的通信协议来 ...

  3. 利用Swoole实现PHP+websocket直播,即使通讯代码,及linux下swoole安装基本配置

    swoole安装基本配置 php安装swoole 1. 下载swoole安装 wget http://pecl.php.net/get/swoole-1.9.1.tgz tar -zxvf swool ...

  4. Struts2架构流程

    [Struts2] Action实现. interceptor实现. Filter工作原理. 使用 拦截器来处理请求. 业务逻辑控制器与 Servlet API分离. ================ ...

  5. window.name 跨域数据传输

    通过window.name可以实现跨域数据传输. 要解决的功能:  www.a.com/a.html 需要获取到 www.b.com/b.html页面内容的数据 需要3个页面 www.a.com/a. ...

  6. 51nod 1272 思维/线段树

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...

  7. SQL Server循环插入数据

    --循环执行插入10000条数据declare @ID intdeclare @eigyousyocode nvarchar(16)declare @datet datetimedeclare @pl ...

  8. github 第一次使用及出现的问题解决

    1.前言: 我们使用git,自然是希望我们的项目可以方便的从本地上传到git的仓库中,从而实现项目版本控制和备份,但是,从GitHub的网站上传文件,只能上传25MB的数据,我想多数人的项目都不可能只 ...

  9. java学习笔记 --- 多线程(1)

    1:要想了解多线程,必须先了解线程,而要想了解线程,必须先了解进程,因为线程是依赖于进程而存在. 2:什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出现进 ...

  10. unity 四元数, 两行等价的代码

    Vector3 tmpvc; 1. tmpvc = Quaternion.Euler (new Vector3 (0, 30, 0)) * new Vector3 (0, 0, 1); 2. tmpv ...