您的位置:首页 > 其它

怎么计时程序运行的时间

2013-10-25 10:35 459 查看
【问题描述】程序运行时间是编程的一项重要指标,如何测算程序的运行时间呢?

【解析】

测试函数

[html] view
plaincopy

#include <math.h>



void function()

{

unsigned int i,j;

double y;



for(i=0;i<1000;i++)

for(j=0;j<1000;j++)

y=sin((double)i);

}



方法1 如果在QT中,利用QTime,其精度为ms级----------在QT中使用。

[html] view
plaincopy

#include <QDebug>

#include <QTime>



QTime time;



time.start();

function();



qDebug()<<time.elapsed()/1000.0<<"s";

运行结果:0.109 s

方法2 利用gettimeofday(),其精度为us级----------C语言实现,在Linux下使用。

[html] view
plaincopy

#include <QDebug>

#include <sys/time.h>



struct timeval tpstart,tpend;

float timeuse;



gettimeofday(&tpstart,NULL);

function();

gettimeofday(&tpend,NULL);

timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;



qDebug()<<timeuse<<"s";

运行结果:0.109375 s

方法3 利用clock(),其精度为ms级----------C++实现,在VC和QT中也可以使用。


[html] view
plaincopy

#include <QDebug>

#include <sys/time.h>



double time_Start = (double)clock();

function();

double time_End = (double)clock();



qDebug()<<(time_End - time_Start)/1000.0<<"s";

运行结果:0.11 s

方法4 利用windows.h(VC)函数,其精度为us级----------在VC和QT中使用。

[html] view
plaincopy

#include <QDebug>

#include <windows.h>



LARGE_INTEGER litmp;

LONGLONG Qpart1,Qpart2,Useingtime;

double dfMinus,dfFreq,dfTime;



//获得CPU计时器的时钟频率

QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),

dfFreq = (double)litmp.QuadPart;



QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值

Qpart1 = litmp.QuadPart; //开始计时



function(); //待测试的计算函数等



QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值

Qpart2 = litmp.QuadPart; //终止计时



dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值

dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)

Useingtime = dfTime*1000000;



qDebug()<<dfTime<<"s";

运行结果:0.107415 s



【代码清单】

[html] view
plaincopy

#include <QDebug>

#include <QTime>

#include <sys/time.h>

#include <windows.h>

#include <math.h>



void function();



int main(void)

{

qDebug()<<"-------------------------------";

//-1-

QTime time;

time.start();

function();

qDebug()<<time.elapsed()/1000.0<<"s";



//-2-

struct timeval tpstart,tpend;

float timeuse;

gettimeofday(&tpstart,NULL);

function();

gettimeofday(&tpend,NULL);

timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;

qDebug()<<timeuse<<"s";



//-3-

double time_Start = (double)clock();

function();

double time_End = (double)clock();

qDebug()<<(time_End - time_Start)/1000.0<<"s";



//-4-

LARGE_INTEGER litmp;

LONGLONG Qpart1,Qpart2,Useingtime;

double dfMinus,dfFreq,dfTime;



//获得CPU计时器的时钟频率

QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),

dfFreq = (double)litmp.QuadPart;



QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值

Qpart1 = litmp.QuadPart; //开始计时



function(); //待测试的计算函数等



QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值

Qpart2 = litmp.QuadPart; //终止计时



dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值

dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)

Useingtime = dfTime*1000000;



qDebug()<<dfTime<<"s";



return 0;

}



void function()

{

unsigned int i,j;

double y;



for(i=0;i<1000;i++)

for(j=0;j<1000;j++)

y=sin((double)i);

}

----------------------------------------------------------------------------
另外,方法2可具体参考:

在测试程序时,我们往往需要了解程序执行所需的时间,在C语言可以使用函数gettimeofday来得到时间,它的调用格式是:

#include <sys/time.h>int gettimeofday(struct timeval *tv,struct timezone *tz);int settimeofday(const struct timeval *tv , const structtimezone *tz);

其中结构timeval的定义为:

strut timeval {long tv_sec;long tv_usec;};

可以看出,使用这种方式计时,精度可达微秒。

进行计时的时候,我们需要前后调用两次gettimeofday,然后计算中间的差值,实例代码如下:

************************************

gettimeofday( &start, NULL );

foo(); //示例函数

gettimeofday( &end, NULL);

timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;

timeuse /=1000000;

***********************************

其中start,end 是结构体所定义的timeval型。

--------------------------------------------------------------------------------
方法3可具体参考:

我现在用C++语言写了一段程序,想计算这段程序运行的准确时间,这是要用于跟其它实验结果作对比的,所以要精确到毫秒,C++程序运行时间 确实很难掌握啊!

C++程序运行时间中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:
#ifndef   _CLOCK_T_DEFINED     
 typedef   long   clock_t;     
 #define   _CLOCK_T_DEFINED     
 #endif


这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(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,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
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毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的C++程序运行时间 :
void   elapsed_time()     
  {     
  printf("Elapsed   time:%u   secs."n",clock()/CLOCKS_PER_SEC);     
  }


当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
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毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一 些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C++程序运行时间 中,最小的计时单位是一毫秒。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: