Application Performance: C # vs C/C ++

Source: Internet
Author: User

Recently, I have been doing ACM on the timus online judge website.

First, let's take a look at timus 1114. boxes:

This question requires calculating the number of combinations of balls of the two colors in the box. We found that the sameAlgorithm, C #ProgramIt is 62 times slower than the C ++ program.

Is it true that the performance of C # applications must be poor? No. In fact, the algorithm used in this question is very efficient. The above 0.001 S and 0.062 s are the minimum time for the C/C ++ program and the C # program to run on the timus online judge website respectively. After all, C # Is a hosted application. To run it in the CLR environment, JIT compilation is required during the first running. The minimum basic overhead is larger than that of C/C ++ applications.

Next, let's take a look at timus 1219. Symbolic sequence:

This question requires the output of 1 million lowercase Latin letters that meet the given conditions. Using the same algorithm, C # programs are 15 times slower than C ++ programs, and 64 times slower than C Programs.

This time, it cannot be explained with the minimum basic overhead, because the running time of these programs is not very short. However, this question is quite special, and its time is mainly spent on outputting a large number of (1 million characters) characters. C # The program calls the console 1 million times. in the write () method, the C ++ program calls the 1 million STD: cout <C statement, and the C program calls the putchar () function for 1 million times. The difference should be caused by the different efficiency of the three methods. If the algorithm of this question is slightly modified so that the C # program only calls the console. Write () method once to output all 1 million characters, the running time will be reduced from 0.968 seconds to 0.093 seconds.

Now let's take a look at timus 1152. The false mirrors:

This story tells the story about the elimination of monsters, and requires the calculation of the minimum damage to the protagonist in the story. Using the same algorithm, we finally see that the running time of the C # program is almost the same as that of the C ++ program.

However, frankly speaking, the algorithm I actually use is not optimal. The Optimal Algorithm for this question is implemented in C ++, and the running time is only 0.001 seconds. I don't know what the algorithm is. If anyone knows it, let me know. :)

Because most ACM questions use good algorithms for a short period of time, if you use C # language to do the exercises, you will find that it is much slower than the C/C ++ language, but in general, it will not time out unless you are using a poor algorithm. The following is an example of timus 1081. Binary lexicographic sequence:

The K (0 <K<109) n (0 <N<44) bit binary number, which must not have adjacent "1 ". In the C # And C ++ programs of accepted, the time complexity is O (n. In the time limit exceeded C ++ program, the time complexity is about O (1.618n + 2.

The key is algorithms, not programming languages.

Furthermore, the performance of hosted applications may actually exceed that of unmanaged applications in some application scenarios. For example, when the JIT compiler runsCodeDuring code compilation, the compiler has a deeper understanding of the execution environment than the unmanaged compilation.

The JIT compiler can determine whether an application runs on a core 2 Duo CPU and generate local code to use any special commands supported by Core 2 Duo. Generally, unmanaged applications are compiled for CPUs with a minimal set of functions and do not use special commands that can improve application performance.

The JIT compiler may determine whether a particular test always fails on the machine where it runs. For example, assume that a method contains a piece of code to determine that the number of CPUs on the host is more than one. If there is only one CPU on the host, the above Code will cause the JIT compiler to not generate any CPU commands. In this case, the local code will be optimized for the host, and the final code will become smaller and run faster.

When an application is running, CLR can evaluate code execution and re-compile the code at the cost. The re-encoding code may be reorganized to reduce incorrect branch prediction based on the observed execution mode.

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.