C/C++中计算函数运行时间的两种方法

       在写代码中,有时候我们需要评估某段代码或者函数的执行时间;方法就是在该段代码或者函数前面,记录一个时间T1,在代码段或函数后面记录时间T2,那其运行时间就是T2-T1,下面看看具体运算方法:

方法一:

       clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t;  头文件:time.h/ctime


       在C/C++中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
       
        #define    CLOCKS_PER_SEC      ((clock_t)1000)

代码实现:

#include “stdio.h”    
#include “stdlib.h”    
#include “time.h”    
int main( void )    
{    
   long    i = 10000000L;    
   clock_t start, finish;    
   double  duration;    
   /* 测量一个事件持续的时间*/    
   printf( "Time to do %ld empty loops is ", i );    
   start = clock();    
   while( i-- )      ;    
   finish = clock();    
   duration = (double)(finish - start) / CLOCKS_PER_SEC;    
   printf( "%f seconds\n", duration );    
   system("pause");    
}    


  1. 在笔者的机器上,运行结果如下:        
  1. Time to do 10000000 empty loops is 0.03000 seconds  
方法二:

函数原型:DWORD GetTickCount(void);
头文件:    C/C++头文件:winbase.h

windows程序设计中可以使用头文件windows.h

 核心函数                头文件          函数库         精度        准确度

     ftime                sys/timeb.h       c函数           ms         较准确



#include <time.h> 
#include <stdio.h> 
#include <tchar.h> 
#include <sys/timeb.h> 
#include<Windows.h> 
int main()
{
struct timeb startTime, endTime;
ftime(&startTime);
Sleep(1000);
ftime(&endTime);
printf("time: %d ms\n", (endTime.time - startTime.time) * 1000 
+ (endTime.millitm - startTime.millitm));
return 0;
}

方法三:

GetTickCount是函数。GetTickCount返回(retrieve)从操作系统启动所经过(elapsed)的毫秒数,它的返回值是DWORD。


#include<iostream>  
#include<Windows.h>  
  
using namespace std;  
  
int main()  
{  
    DWORD start_time = GetTickCount();  
    for (int i = 0; i < 100000000; i++)  
    {  
        i++;  
    }  
    DWORD end_time = GetTickCount();  
    cout << "The run time is:" << (end_time - start_time) << "ms!" << endl;  
    system("pause");  
    return 0;  
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

askunix_hjh

谢谢请我吃糖~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值