For C, C ++, C #, and Java program speed problems!

Source: Internet
Author: User

I tested the performance of C, C ++, Java, and C.

See the following test tables:

 

From my tests, we can see that on Windows, the best performance is still C/C ++, but C # has a good performance, which is close to C ++.

The performance is inferior to that of Java, but there is not much difference. Java is optimized very well in Recursive Computing, and its performance even exceeds the C and C ++ programs compiled by mingw. However, Java has more performance than C and C ++. From my tests, it is basically impossible. In terms of the sieve of Eratosthenes algorithm, many people on the Internet say that Java is faster, but in my test, Java is still 20%-50% slower than C and C ++. (My test data: the average value of 10 requests for running C ++ 1018 Ms Java 1335 ms)

 

Microsoft's vc2010 code optimization is very good, especially the recursive feature is twice as powerful as that of the mingw compiler! In other aspects, GCC and vs2010 are basically flat.

 

As for C and C ++, the performance of several tests I have done is basically the same. C is a little faster. It can be ignored.

 

My test code implements the same functions in different languages and adopts the same algorithm, but the code is not exactly the same, because some syntaxes of each language are different.

 

The test code is listed below:

First look at the C language:

First, you must implement the time calculation function:

File: stopwatch. h

# Ifndef stopwatch_h_0000ded <br/> # define stopwatch_h_0000ded <br/> void init_stopwatch (); <br/> void start_stopwatch (); <br/> void end_stopwatch (); <br/> double get_stopwatch_ms (); <br/> # endif // stopwatch_h_included 

File: stopwatch. c

# Include <windows. h> <br/> # include "stopwatch. H "<br/> large_integer beginticks; <br/> large_integer endticks; <br/> large_integer frequency; <br/> void init_stopwatch () <br/>{</P> <p> beginticks. quadpart = 0; <br/> endticks. quadpart = 0; <br/> frequency. quadpart = 0; <br/> queryperformancefrequency (& frequency); <br/>}< br/> void start_stopwatch () <br/>{</P> <p> queryperformancecounter (& beginticks); <br/>}< br/> void end_stopwatch () <br/>{</P> <p> queryperformancecounter (& endticks); <br/>}< br/> double get_stopwatch_ms () <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/> 

 

Then the test function is implemented.

Filename: work_test.h

# Ifndef work_test_h_included <br/> # define work_test_h_included <br/> double add_test (); <br/> double mul_test (); <br/> double div_test (); <br/> double maid (); <br/> void auto_test (double (* test_item) (); <br/> # endif // work_test_h_included 

Filename: work_test.c

# Include "work_test.h" <br/> # include "stopwatch. H "<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> double add_test () <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 mul_test () <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 div_test () <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 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 fig () <br/>{< br/> return (double) fib (30 ); <br/>}< br/> void auto_test (double (* test_item) () <br/>{< br/> const int test_count = 10; <br/> double test_time = 0.0, ret = 0.0; <br/> int run_count = 0; <br/> init_stopwatch (); <br/> for (run_count = 0; run_count <test_count; run_count ++) <br/>{< br/> ret = 0.0; <br/> start_stopwatch (); <br/> ret = (* test_item) (); <br/> If (Ret> 0) end_stopwatch (); <br/> else end_stopwatch (); <br/> test_time + = get_stopwatch_ms (); <br/>}< br/> printf ("work cost average time (10) = % F/N", test_time/test_count); <br/>}< br/> 

 

 

 

 

 

 

 

 

 

 

 

 

Finally, the main function implementation:

Filename: Main. c

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include "work_test.h" <br/> int main () <br/> {<br/> auto_test (add_test ); <br/> auto_test (FIG); <br/> auto_test (mul_test); <br/> auto_test (div_test); <br/> return 0; <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.