C/C++下测量函数运行时间
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++下测量函数运行时间的更多相关文章
- python测量函数运行时间长度
python测试函数运行时间长度的方法如下 import time def measure_time(): def wraps(func): def mesure(*args,**kwargs): s ...
- linux下统计程序/函数运行时间(转)
一. 使用time 命令 例如编译一个hello.c文件 #gcc hello.c -o hello 生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令 #time ./hello ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数009,Measure,测量函数
<zw版·Halcon-delphi系列原创教程> Halcon分类函数009,Measure,测量函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替 ...
- 用SWD调试接口测量代码运行时间 ( SWO )
用SWD调试接口测量代码运行时间 关于时间测量的种种问题 在嵌入式中,我们经常需要测量某段代码的执行时间或测量事件触发的时间,常规的思路是: 1:在测量起始点,反转电平2:在测量结束点,再次反转电平 ...
- [Android Memory] Linux下malloc函数和OOM Killer
http://www.linuxidc.com/Linux/2010-09/28364.htm Linux下malloc函数主要用来在用户空间从heap申请内存,申请成功返回指向所分配内存的指针,申请 ...
- linux和window下mkdir函数问题(转-锦曦月)
通过WIN32宏进行判断 window下mkdir函数 #include<direct.h> int _mkdir( const char *dirname ); linux下 ...
- linux下syscall函数,SYS_gettid,SYS_tgkill
出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html linux下syscall函数,SYS_gettid,SYS_tgkill ...
- hdwiki model目录下的函数类
model目录下的函数类 actions.class.php(站内地图相关) getHTML:获得页面菜单和相关信息 getMap:生成站内地图 adv.class.php 对wiki_adve ...
- 对于linux下system()函数的深度理解(整理)
原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...
随机推荐
- DecimalFormat数据格式函数
DecimalFormat数据格式函数 class FormatDemo2{ public void format(String pattern, double value) { DecimalFor ...
- ural 2020 Traffic Jam in Flower Town(模拟)
2020. Traffic Jam in Flower Town Time limit: 1.0 secondMemory limit: 64 MB Having returned from Sun ...
- 51nod 1449 贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...
- Vue中mixin的用法
在项目中我们经常会遇到多个组件调用同一个方法的问题,为了避免每次都在.vue文件中定义并调用,我们可采用vue的mixin的用法: 具体使用如下: 我们需要在main.js中引入mixins文件夹下的 ...
- Javasript 内置函数
var str = 'AAAA';var aTest= new Array(); //['ff'[,'er']] \ new Array(10); \ new Array('ff','fee');va ...
- C/C++ 字符串与数字相互转换
一.利用stringstream类 1. 字符串到整数 stringstream sstr(str); int x; sstr >> x;(即从sstr中提取数据) ...
- Azure新建的CentOS设置root账户的密码
前言:Azure在新建VM的时候的账户使用的是自定义的用户名和密码或者自定义的用户名使用公钥 1.使用自定义的用户名登录到服务器. 2.设置root的密码: sudo passwd root 3.按照 ...
- codewar代码练习2——7级晋升6级
7级晋升到6级的过程中以做6级题以及以前未完成的题目为主,一般选择算法题或者基础题.相比之前从8级升级7级(参见此博客:http://blog.csdn.net/m0_37324740/article ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- 转载:jquery插件实现图片延迟加载(lazyload.js)
转载: http://www.cnblogs.com/tinyphp/archive/2013/04/09/3009385.html