When different types of data are mixed in a C ++ application, the C ++ compiler automatically performs type conversion. To avoid Type Problems in operations of different data types, try to use the same type of data, rather than using special commands that can improve application performance.
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 ++ applications 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, the C ++ application calls the Console only once. if the Write () method outputs all 1 million characters, the running time decreases 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:
This question requires that the number of K (0 <K <109) N (0 <N <44) bits must be given. The number of BITs must not have an adjacent "1 ". In the C # And C ++ programs of Accepted, the time complexity is O (N. In the Time limit exceeded C ++ program, an algorithm with a Time complexity of about C ++ is used.
Furthermore, the performance of hosted applications may actually exceed that of unmanaged applications in some application scenarios. For example, when the JIT compiler compiles the IL code at runtime, the compiler has a deeper understanding of the execution environment than the unmanaged compilation.
- This article describes how to build a C ++ environment in LINUX.
- Summary Notes on learning and exploring C ++ library functions
- In-depth analysis of the working mechanism of C ++ virtual tables
- C ++ security issues under various conditions
- How to Write C ++ project development and project plan correctly
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.