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. UML类图(二)--------类与类之间的关系之依赖,继承,接口与实现关系

     依赖(Dependency)关系是一种使用关系,特定事物的改变有可能会影响到使用该事物的其他事物,在需要表示一个事物使用另一个事物时使用依赖关系.大多数情况下,依赖关系体现在某个类的方法使用另一个类 ...

  2. zoj 1375 贪心

    https://vjudge.net/problem/ZOJ-1375 In modern day magic shows, passing through walls is very popular ...

  3. MySql简单分页存储过程

    BEGIN DECLARE startIndex int; select COUNT(*) INTO RecordCount from test; SET startIndex = (PageInde ...

  4. oralce 索引(1)

    本文来自网上整理 来自以下博客内容 http://www.360doc.com/content/13/0712/11/13136648_299364992.shtml; http://www.cnbl ...

  5. LeetCode OJ:Subsets(子集)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  6. linux monitor and maintanence

    @cacti 1.install epel extends source 2.install lamp use yum method yum install -y httpd php php-mysq ...

  7. uva11806(容斥原理)

    11806 - Cheerleaders Time limit: 2.000 seconds In most professional sporting events, cheerleaders pl ...

  8. tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

  9. HUST 1010 The Minimum Length

    There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I go ...

  10. 畅通工程再续 (kruskal算法的延续)

    个人心得:这题其实跟上一题没什么区别,自己想办法把坐标啥的都给转换为对应的图形模样就好了 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实 ...