For C, C ++, C #, and Java program speed problems! (2)

Source: Internet
Author: User

The code for C ++ is listed below:

 

1. Implement the stopwatch class for measuring time

/* <Br/> filename: stopwatch. h <br/> */<br/> # ifndef stopwatch_h_0000ded <br/> # define stopwatch_h_0000ded <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; // high-performance counter frequency: 357,9545 tick per second my 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/> querycececounter (& beginticks ); </P> <p >}< br/> void stopwatch: Stop () <br/>{</P> <p> queryperformancecounter (& endticks ); <br/>}< br/> double stopwatch: getcostmillisecond () <br/>{< br/> unsigned long cost = (unsigned long) (endticks. quadPart-beginticks.QuadPart); <br/> double millsecond = (double) Cost * 1000.0/(double) frequency. quadpart; <br/> return millsecond; <br/>}< br/> 

 

2. Implementation Testing

 

// 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 fiber test (); <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/> // CTO R <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: fiber test () <br/>{< br/> return (double) FIB (30); <br/>}</P> <p> 

 

3. implement automatic test class, test 10 times, get average score

// 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. Finally, the program entry function

 

# 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: fiber test); <br/> ATC. autotest (work_c, & workclass: multest); <br/> ATC. autotest (work_c, & workclass: divtest); </P> <p> return 0; <br/>} 

 

Note that the optimization of vs2010 may result in an incorrect stopwatch value. Therefore, the related code is added to prevent timing errors caused by vs2010 optimization.

(This should be the bug of vs2010 optimization. vs2010 optimization is really amazing, but it is too much .)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.