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. Oracle创建表空间和增加表空间

    1.创建表空间 create tablespace fgq datafile 'E:\app\Administrator\oradata\fms\fgq01.dbf' size 1000M autoe ...

  2. hadoop环境搭建(linux单机版)

    一.在Ubuntu下创建hadoop用户组合hadoop用户 1.创建hadoop用户组      addgroup hadoop 2.创建hadoop用户      adduser -ingroup ...

  3. Rest Web Api Controller 返回JSON格式大小写

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  4. 如何激活Windows10系统

    Win10正式企业版系统的激活方法: 按住 win+x 就会出现如下,右击桌面的左下角的“Windows”图标,从其右键菜单中选择“命令提示符(管理员)”项,以便打开 MSDOS界面.   待打开MS ...

  5. 51nod 1267 二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1267 1267 4个数和为0 基准时间限制:1 秒 空间限制:13107 ...

  6. Tomcat翻译--Context Container

    原文:http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions The Context Cont ...

  7. ps6-图层基础与操作技巧

    1.图层的新建.复制与删除 ctrl+j:复制图层,可以用复制选区作为新图层 Shift+Ctrl+Alt+e:在新的空白图层将下面所有的图层合并为一个图层. 2.选择复制与链接图层 在移动图层时,按 ...

  8. UVALive - 3211 Now or later (二分+2SAT)

    题目链接 题意:有n架飞机,每架飞机有两个着陆时间点可以选,要求任意两架飞机的着陆时间之差不超过k,求k的最大值. 解法:由于每架飞机都有两个选择,并且必选且只能选其中一个,时间冲突也是发生在两架飞机 ...

  9. LeetCode 362. Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...

  10. NOIp2018集训test-10-24(am&pm)

    李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...