下面列出C++的代碼:
1.實現StopWatch類,做測量時間用
/*<br />filename:stopwatch.h<br />*/<br />#ifndef STOPWATCH_H_INCLUDED<br />#define STOPWATCH_H_INCLUDED<br />#include<Windows.h><br />class StopWatch<br />{<br />public:<br /> StopWatch(void);<br /> ~StopWatch(void);<br />private:<br /> LARGE_INTEGER beginticks;<br /> LARGE_INTEGER endticks ;<br /> LARGE_INTEGER frequency;//高效能計數器的頻率:每秒357,9545個tick 我的INTEL T7500</p><p>public:<br /> void Start();<br /> void Stop();<br /> double GetCostMillisecond();</p><p>};<br />#endif // STOPWATCH_H_INCLUDED</p><p>/****************************************<br />filename:StopWatch.cpp<br />*****************************************/<br />#include "StopWatch.h"<br />#include<iostream><br />using namespace std;<br />StopWatch::StopWatch(void)<br />{<br /> beginticks.QuadPart=0;<br /> endticks.QuadPart=0;<br /> frequency.QuadPart=0;<br /> QueryPerformanceFrequency(&frequency);</p><p>}<br />StopWatch::~StopWatch(void)<br />{<br />}<br />void StopWatch::Start()<br />{<br /> //beginticks=GetTickCount();<br /> QueryPerformanceCounter(&beginticks);</p><p>}<br />void StopWatch::Stop()<br />{</p><p>QueryPerformanceCounter(&endticks);<br />}<br />double StopWatch::GetCostMillisecond()<br />{<br /> unsigned long long cost=(unsigned long long)(endticks.QuadPart-beginticks.QuadPart);<br /> double millsecond=(double)cost*1000.0/(double)frequency.QuadPart;<br /> return millsecond;<br />}<br />
2.實現測試工作類
//filename:WorkClass.h<br />#ifndef WORKCLASS_H<br />#define WORKCLASS_H</p><p>class WorkClass<br />{<br /> public:<br /> WorkClass();<br /> ~WorkClass();<br /> double AddTest();<br /> double FibTest();<br /> double MulTest();<br /> double DivTest();</p><p> protected:<br /> private:<br /> unsigned long fib(unsigned long n);<br />};<br />#endif<br />//filename:WorkClass.cpp<br />#include "../include/WorkClass.h"<br />#include "../stopwatch.h"<br />#include <iostream><br />using namespace std;<br />WorkClass::WorkClass()<br />{<br /> //ctor<br />}<br />WorkClass::~WorkClass()<br />{<br /> //dtor<br />}<br />double WorkClass::AddTest()<br />{<br /> int count=10000000,i=0,j=0,count2=10;<br /> double sum=0.0;<br /> for(i=0;i<count2;i++)<br /> for(j=0;j<count;j++)<br /> sum+=j;<br /> return sum;<br />}<br />double WorkClass::MulTest()<br />{<br /> int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum*=i;<br /> return sum;<br />}<br />double WorkClass::DivTest()<br />{<br /> int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum/=i;<br /> return sum;<br />}<br />unsigned long WorkClass::fib(unsigned long n)<br />{<br /> if (n < 2)<br />return(1);<br /> else<br />return(fib(n-2) + fib(n-1));<br />}<br />double WorkClass::FibTest()<br />{<br />return (double)fib(30);<br />}</p><p>
3.實現自動化的測試類,測試10次,取平均成績
//filename:AutoTestClass.h<br />#ifndef AUTOTESTCLASS_H<br />#define AUTOTESTCLASS_H<br />#include "WorkClass.h"<br />class AutoTestClass<br />{<br /> public:<br /> AutoTestClass();<br /> ~AutoTestClass();<br /> void AutoTest(WorkClass &wc,double (WorkClass::*item)());<br /> protected:<br /> private:<br />};<br />#endif // AUTOTESTCLASS_H</p><p>//filename:AutoTestClass.cpp<br />#include "../include/AutoTestClass.h"<br />#include "../stopwatch.h"<br />#include<iostream><br />using namespace std;<br />AutoTestClass::AutoTestClass()<br />{<br /> //ctor<br />}<br />AutoTestClass::~AutoTestClass()<br />{<br /> //dtor<br />}<br />void AutoTestClass::AutoTest(WorkClass &wc,double (WorkClass::*item)())<br />{<br /> const int test_count=10;<br /> double test_time=0.0,ret=0.0;<br /> int run_count=0;<br /> StopWatch sw;<br /> for(run_count=0;run_count<test_count;run_count++)<br /> {<br /> ret=0.0;<br /> sw.Start();<br /> ret=(wc.*item)();<br /> if(ret>0)sw.Stop();<br /> else sw.Stop();<br /> test_time+=sw.GetCostMillisecond();<br /> }<br /> cout<<"work cost average time(10)="<<test_time/test_count<<endl;<br />}<br />
4.最後是程式入口函數
#include"include/WorkClass.h"<br />#include"include/AutoTestClass.h"<br />#include <iostream><br />using namespace std;<br />int main()<br />{<br /> WorkClass work_c;<br /> AutoTestClass atc;<br /> atc.AutoTest(work_c,&WorkClass::AddTest);<br /> atc.AutoTest(work_c,&WorkClass::FibTest);<br /> atc.AutoTest(work_c,&WorkClass::MulTest);<br /> atc.AutoTest(work_c,&WorkClass::DivTest);</p><p> return 0;<br />}
C和C++要注意VS2010的最佳化可能導致StopWatch取值錯誤。所以特別加了相關性代碼,防止VS2010最佳化而導致的計時錯誤。
(這應該是VS2010最佳化的BUG,VS2010最佳化確實很厲害,可惜這點過頭了。)