算法时间测试

 1 //算法时间测试 基准法(定量分析) 大O法(定性分析)
 2 //以下是基准法
 3 #include "iostream"
 4 #include "cstdio"
 5 #include "ctime"
 6 #include "windows.h"
 7 using namespace std;
 8 int main()
 9 {
10     //法一
11     clock_t start1,end1;//clock_t  需头文件#include "ctime"
12     double duration1 ;
13 
14     start1=clock();//返回程序运行到此位置时所用毫秒数
15     ::Sleep(3000);//延时函数 参数为毫秒  头文件#include "windows.h"   注俩冒号和大写S
16     end1=clock();
17     duration1=(double)(end1-start1)/CLOCKS_PER_SEC;//为输出秒,毫秒化秒
18     cout<<"The clock_t  time is  "<<duration1<<"S"<<endl;
19 
20 
21 
22     //法二
23     time_t start2,end2;
24 
25     start2=time(0);//获取的是从1970 1 1 0:0:0 到现在的秒数(注意单位为秒)
26     ::Sleep(3000);
27     end2=time(0);
28     double duration2=end2-start2;
29     cout<<"The time_t   time is  "<<duration2<<"S"<<endl;
30 }
View Code

 感谢http://blog.csdn.net/theprinceofelf/article/details/6636041

 1 这个是针对于特定的平台的测试,有时候我们需更高的精度测试时,往往需要用到这个测试
 2 <Windows.h>版本
 3 #include "windows.h"
 4 int main()
 5 {
 6     LARGE_INTEGER frec;//记录CPU每秒频率
 7     LARGE_INTEGER strt;
 8     LARGE_INTEGER ed;
 9     QueryPerformanceFrequency(&frec);///查询频率
10     QueryPerformanceCounter(&strt);///测开始处的时钟数
11     ::Sleep(3000);
12     QueryPerformanceCounter(&ed);///测结束处的时钟数
13     cout<<(ed.QuadPart-strt.QuadPart)*1000000/frec.QuadPart<<"us"<<endl;///相减除以每秒的频率
14     ///。*1是秒数,*1000是毫秒数,*1000 000 是微妙数,*1000 000 000是ns数。
15 }

 好的代码习惯:测试代码 -->  伪代码 --> 实际代码 --> 更加高效的版本 --> 带输入输出控制检测,出错验证的代码  -->更加灵活的版本

原文地址:https://www.cnblogs.com/kimsimple/p/5672091.html