怎么计时程序运行的时间
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函数定义如下:
这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义:
很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的C++程序运行时间 :
当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一 些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C++程序运行时间 中,最小的计时单位是一毫秒。
【解析】
测试函数
[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++程序运行时间 中,最小的计时单位是一毫秒。
相关文章推荐
- Java程序运行时间计时代码
- msgbox框出来以后,如果一定时间内没有人点确定,那怎么让这个框消失,使程序自动运行?
- C# 时间(几个常用时间,程序运行计时,页面运行计时).txt
- python程序运行时间计时
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- c++计算程序运行时间的函数,怎么才能精确到微妙级呢?
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C#时间函数(程序运行计时,页面运行计时)
- 怎么看java程序的运行时间
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- 编写一个C语言语句,要求输出这个程序需要运行的时间(用clock()做) 该怎么写?
- C/C++如何以秒计时程序运行时间
- PAT1026程序运行时间
- C#中提供的精准测试程序运行时间的类Stopwatch
- 计算Python的代码块或程序的运行时间
- 实现Java程序间隔时间运行