C++程序运行时间-ZZ

【15.5.25】贴一段实用的代码,linux下跑过。

 

 1 #include <stdio.h>      /* printf */
 2 #include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
 3 #include <math.h>       /* sqrt */
 4 #include <iostream>
 5 using namespace std;
 6 
 7 int frequency_of_primes (int n) {
 8   int i,j;
 9   int freq=n-1;
10   for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
11   return freq;
12 }
13 
14 int main ()
15 {
16   clock_t t;
17   int f;
18   t = clock();
19   printf ("Calculating...\n");
20   f = frequency_of_primes (99999);
21   printf ("The number of primes lower than 100,000 is: %d\n",f);
22   t = clock() - t;
23   printf ("It took me %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
24   cout<<t<<","<<(float)t/CLOCKS_PER_SEC;
25   return 0;
26 }

 

reference to cplusplus.

 

 

 

 

 

 

http://www.cnblogs.com/houkai/archive/2013/06/06/3120768.html

http://www.douban.com/note/224133728/

------------------------------------------------------------------------------------

在C++程序的性能分析中,其中重要的一项就是程序的运行时间。虽然程序的运行速度与计算机的配置、计算机的当前状态等有很大关系,但在相对一致的外部环境下,程序运行时间的长短在很大程度上是可以反映程序效率的。

1.一般计时方法

在ctime头文件中,C++提供了计时函数 clock() ,其返回数据类型为 clock_t。

typedef   long   clock_t;

clock()函数返回从“开启程序进程”到“程序中调用clock()函数”这段时间里,CPU时钟计时单元(clock tick)的数目,在MSDN中称之为挂钟时间(wal-clock)。 
在ctime文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒内有多少个CPU时钟计时单元。 
通过clock()/CLOCKS_PER_SEC便可以得到进程的运行时间,一般CLOCKS_PER_SEC的值为1000,可见计时精度可达小数点后3位(毫秒级)。

#define CLK_TCK  CLOCKS_PER_SEC

计时的简单示例如下

复制代码
/*
 *作者:侯凯
 *说明:clock()计时函数
 *日期:2013-6-6
*/
#include <ctime> //计时用的头文件
#include <iostream>
using namespace std;

int main()
{
    long i = 10000000L;
    clock_t start, end; 
    
    start = clock(); 
    while( i-- );//需要计时的程序段
    end = clock(); 
    printf("The time was: %f\n", (double)(end - start) / CLK_TCK); 

    system("pause");
    return 0;
}
复制代码

2.精确计时方法

这里精确的含义是计时的精度更高,为了达到更高的计时精度,需要使用精确时间函数QueryPerformanceCounter()和QueryPerformanceFrequency(),它们需要计算机从硬件上支持精确定时器。当然,现在计算机一般都是支持的。 
QueryPerformanceCounter()函数返回高精确度计数器的脉冲数目(计时数),QueryPerformanceFrequency()函数提供了高精度计时器的频率值,即每秒脉冲数。

bool  QueryPerformanceFrequency (LARGE_INTEGER *lpFrequency);
bool  QueryPerformanceCounter (LARGE_INTEGER *lpCount);

其中,数据类型LARGE_INTEGER既可以是一个8字节长的整型数,也可以是两个4字节长的整型数的联合结构, 其具体用法根据编译器是否支持64位而定。该类型的定义如下:

复制代码
typedef union _LARGE_INTEGER
{
     struct
     {
       DWORD LowPart ;// 4字节整型数
        LONG  HighPart;// 4字节整型数
     };
     LONGLONG QuadPart ;// 8字节整型数
     
 }LARGE_INTEGER ;
复制代码

在进行定时之前,先调用QueryPerformanceFrequency()函数获得机器内部定时器的时钟频率,然后在需要严格定时的事件发生之前和发生之后分别调用QueryPerformanceCounter()函数,利用两次获得的计数之差及时钟频率,计算出事件经历的精确时间。其过程与clock()方法类似,但这里的时钟频率很高,测试电脑上频率的QuadPart值为3118031,其精度原高于clocK()。 
为了更好地使用这种计时方式,已将其封装成HpTime类,下载。使用该类,计时程序如下

复制代码
/*
 *作者:侯凯
 *说明:HpTime类高精度计时
 *日期:2013-6-6
*/
#include "hptime.h"
#include <iostream>
using namespace std;

int main()
{
    long i = 10000000L;
    HpTime hpTime;
    
    while( i-- );//要计时的函数段

     printf("The time was: %f\n", hpTime.sec()); 
    system("pause");
    return 0;
}
复制代码
 
 
posted @ 2014-11-16 09:29  Ryan in C++  阅读(266)  评论(0编辑  收藏  举报