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

Source: Internet
Author: User

The C # code is provided here:

1. Since C # already comes with the stopwatch class, this is saved.

 

2. The first is workclass test class implementation

 

// Filename: workclass. CS <br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace csharp_performance_test <br/>{< br/> class workclass <br/>{< br/> Public double 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/> Public double 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/> Public double 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/> private ulong fib (ulong N) <br/>{< br/> If (n <2) <br/> return (1 ); <br/> else <br/> return (FIB (n-2) + fib (n-1); <br/>}< br/> Public double fiber test () <br/>{< br/> return (double) fib (30); <br/>}< br/> 

 

3. then implement the autotestclass class.

// Filename: autotestclass. CS <br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. diagnostics; <br/> using system. reflection; <br/> namespace csharp_performance_test <br/>{< br/> class autotestclass <br/>{< br/> Public void Autotest (string mname) <br/>{< br/> const int test_count = 10; <br/> double test_time = 0.0, ret = 0.0; <br/> int run_count = 0; <br/> stopwatch Sw = new stopwatch (); <br/> workclass WC = new workclass (); <br/> type mytype = typeof (workclass ); <br/> methodinfo myminfo = mytype. getmethod (mname); <br/> for (run_count = 0; run_count <test_count; run_count ++) <br/>{< br/> ret = 0.0; <br/> SW. reset (); <br/> SW. start (); <br/> ret = (double) myminfo. invoke (WC, null); <br/> If (Ret> 0) SW. stop (); <br/> else SW. stop (); <br/> test_time + = Sw. elapsedmilliseconds; <br/>}< br/> console. writeline ("work cost average time (10) = {0} ms", test_time/test_count ); <br/>}< br/> 

 

4. Finally, the main program

<Br/> // filename: program. CS <br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. diagnostics; <br/> namespace csharp_performance_test <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> autotestclass ATc = new autotestclass (); <br/> ATC. autotest ("addtest"); <br/> ATC. autotest ("fiber test"); <br/> ATC. autotest ("multest"); <br/> ATC. autotest ("divtest"); <br/>}< br/>} 

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.