关于C和C++,还有c#,还有java程序的速度问题!(二)

下面列出C++的代码:

 

1.实现StopWatch类,做测量时间用

/* filename:stopwatch.h */ #ifndef STOPWATCH_H_INCLUDED #define STOPWATCH_H_INCLUDED #include<Windows.h> class StopWatch { public: StopWatch(void); ~StopWatch(void); private: LARGE_INTEGER beginticks; LARGE_INTEGER endticks ; LARGE_INTEGER frequency;//高性能计数器的频率:每秒357,9545个tick 我的INTEL T7500 public: void Start(); void Stop(); double GetCostMillisecond(); }; #endif // STOPWATCH_H_INCLUDED /**************************************** filename:StopWatch.cpp *****************************************/ #include "StopWatch.h" #include<iostream> using namespace std; StopWatch::StopWatch(void) { beginticks.QuadPart=0; endticks.QuadPart=0; frequency.QuadPart=0; QueryPerformanceFrequency(&frequency); } StopWatch::~StopWatch(void) { } void StopWatch::Start() { //beginticks=GetTickCount(); QueryPerformanceCounter(&beginticks); } void StopWatch::Stop() { QueryPerformanceCounter(&endticks); } double StopWatch::GetCostMillisecond() { unsigned long long cost=(unsigned long long)(endticks.QuadPart-beginticks.QuadPart); double millsecond=(double)cost*1000.0/(double)frequency.QuadPart; return millsecond; }  

 

2.实现测试工作类

 

//filename:WorkClass.h #ifndef WORKCLASS_H #define WORKCLASS_H class WorkClass { public: WorkClass(); ~WorkClass(); double AddTest(); double FibTest(); double MulTest(); double DivTest(); protected: private: unsigned long fib(unsigned long n); }; #endif //filename:WorkClass.cpp #include "../include/WorkClass.h" #include "../stopwatch.h" #include <iostream> using namespace std; WorkClass::WorkClass() { //ctor } WorkClass::~WorkClass() { //dtor } double WorkClass::AddTest() { int count=10000000,i=0,j=0,count2=10; double sum=0.0; for(i=0;i<count2;i++) for(j=0;j<count;j++) sum+=j; return sum; } double WorkClass::MulTest() { int count=10000*10000,i=0; double sum=0.0; for(i=1;i<count;i++)sum*=i; return sum; } double WorkClass::DivTest() { int count=10000*10000,i=0; double sum=0.0; for(i=1;i<count;i++)sum/=i; return sum; } unsigned long WorkClass::fib(unsigned long n) { if (n < 2) return(1); else return(fib(n-2) + fib(n-1)); } double WorkClass::FibTest() { return (double)fib(30); }  

 

3.实现自动测试类,测试10次,取平均成绩

//filename:AutoTestClass.h #ifndef AUTOTESTCLASS_H #define AUTOTESTCLASS_H #include "WorkClass.h" class AutoTestClass { public: AutoTestClass(); ~AutoTestClass(); void AutoTest(WorkClass &wc,double (WorkClass::*item)()); protected: private: }; #endif // AUTOTESTCLASS_H //filename:AutoTestClass.cpp #include "../include/AutoTestClass.h" #include "../stopwatch.h" #include<iostream> using namespace std; AutoTestClass::AutoTestClass() { //ctor } AutoTestClass::~AutoTestClass() { //dtor } void AutoTestClass::AutoTest(WorkClass &wc,double (WorkClass::*item)()) { const int test_count=10; double test_time=0.0,ret=0.0; int run_count=0; StopWatch sw; for(run_count=0;run_count<test_count;run_count++) { ret=0.0; sw.Start(); ret=(wc.*item)(); if(ret>0)sw.Stop(); else sw.Stop(); test_time+=sw.GetCostMillisecond(); } cout<<"work cost average time(10)="<<test_time/test_count<<endl; }  

 

4.最后是程序入口函数

 

#include"include/WorkClass.h" #include"include/AutoTestClass.h" #include <iostream> using namespace std; int main() { WorkClass work_c; AutoTestClass atc; atc.AutoTest(work_c,&WorkClass::AddTest); atc.AutoTest(work_c,&WorkClass::FibTest); atc.AutoTest(work_c,&WorkClass::MulTest); atc.AutoTest(work_c,&WorkClass::DivTest); return 0; } 

 

C和C++要注意VS2010的优化可能导致StopWatch取值错误。所以特别加了相关性代码,防止VS2010优化而导致的计时错误。

(这应该是VS2010优化的BUG,VS2010优化确实很厉害,可惜这点过头了。)

 

 

你可能感兴趣的:(java,C++,c,优化,C#,Integer)