C # and C, C ++, D, Java performance comparison (1)

zhaozj2021-02-16  63

C # and C, C , D, Java performance comparison

(Article from http://mag.vchelp.net) jason

Can C # performance can catch up with the compiled C / C / D and the intermediate code to run Java?

Microsoft releases a variety of languages ​​supported by .NET platform and .Net platform, including C of C #, VB.NET, managed Extensions. The system can simultaneously meet the development of efficient server architecture, and development department Strong multi-functional interface needs.

Like other many programmers, when I take the new development language and the daily language, I have an inexplicable prejudice and doubt. This article will discuss the advantages of .NET platform, especially related C #. We take C # and other development languages ​​in high performance software development, making quantitative performance comparisons.

Because the C # program compilation is divided into two parts: compile into an intermediate code, then interpretation of the intermediate code is executed, so we will not only compare him with pure compilation type C / C / d, will also put him Comparison of Java language with typical (interpretation of intermediate code). Java is undoubtedly a mature language, applied in various computer areas. The Java program is first compiled into an intermediate code, and then interprets the local machine code when running through the Java virtual machine. From this point of view, .NET platform, especially the first C # language, borrowed a lot of Java, including grammar and runtime support.

The complete and fair performance is a huge test of the complete fair performance of the new language (or the runtime of language corresponding library the language of the language). This article is a primitive performance comparison of the basic common point involved in the development of C # and C / C / D in development, and also includes the performance comparison of the libraries belled in each language. include:

1. Run a complete program (turn the time to perform a program)

2. Algorithm and recursive (calculate the peripheral rate and eratorshnes's sieve)

3. Type conversion (int-> float, float-> int, int-> string.string-> int)

4. String comparison

5. String connection

6. (String Specific Symbol Statistics)

Through a series of comparisons, some interesting places and .Net runtime performance of the C # compiler will be revealed.

Background:

Although the language of comparison can be considered a C language series, although these five languages ​​are only more or less different in grammar, they have a huge difference in other aspects. (See Table 1, http://www.digitalmars.com/d/comparison.html). I assume that the reader is already familiar with C / C / Java, and some basic C #. For D language, it is not as famous as other languages, but you can find it.

D language introduction

D Language is a language that Walter Bright is developing, installing and learning this language is small. In essence, the goal of D language is a more successful C / C language, improves the array of variable lengths, the [IUT] attribute of the function parameters, automatic intraline, version control, local function, internal construction unit test, Strict types, etc., including the difference in ignore pointers and references implemented in Java / C #, which is only achieved by template in C . It also supports direct support for existing C functions and APIs. It supports many features similar to Java / C #, such as garbage collection. D Language The essence of C # / Java is the virtual machine support that does not require runtime when D language is run. All features supported by D Language are done when compiling, so you don't need to run additional support at runtime. D Language has now reached the Alpha version, and the future mature version will be much more efficient than Java / C # in most cases, unless JIT optimization is very obvious. So we add D language to our test. In fact, Walter tells us that D is fully capable of running as fast as c code, and due to automatic inline, internal construction arrays, string optimization, non-0 string end tag, etc. He believes that it will exceed C when the D language is officially released.

Before the real test begins, I assume that performance is from high to low, it should be C / C , D, C #, Java. As I have confirmed, I suspect that C # generates high performance code.

Performance comparison

We will test performance in the eleventh different situations described below. The test platform is a Pentium4 of 2G 512M. The operating system is Windows XP Pro. Test is carried out under a set of my own design, in order to meet this article, I will use C # to write this tool. In the absence of other interference processes, it is the tester's grade for each tester to take the fastest and slowest one. In addition to the NOOP tests mentioned below, all tests are completed through the high-accuracy timer computing to complete all internal loops. (Translator Note: For example, in INT-> Float measurement, the conversion of multiple int-> float is run by loop, and then the time required to complete the entire loop is measured). If the code shown by Listing1 is the F2i test completed with C #. (C by calling the win32 api function QueryPerformanceCounter (); C by WinSTL of performance_counter; C # by SynSoft.Performance.PerformanceCounter; D by syncsoft.win32.perf.PerfomanceCounte; Java by System.currentTimeMillis ()). Each program contains the adverse effects of "warm-up code" before entering the substantive test code to reduce the adverse effects of factors such as program initialization, carabch.

The compiler version number is as follows:

C #: VC # .NET V7.00.9466, .NET Framework v1.0.3705;

D: DIGITAL MARS D Compiler Alpha V0.61

Java: J2KDSE 1.4.1

C / C is completed under two compilers, named Digital Mars C / C V8.33, STL Port V4.5, and Inter C / C V7.0, header files and libraries VC 6.0 use DIGITAL MARS compiler Yes: First it can quickly generate high quality code, and free. Secondly, for comparison with D language, because D language is used to use the same C / C connector and a lot of C libraries.

For thorough performance comparisons, all compilers have used speed optimal optimization solutions. The Intercom is -QAXW, which provides two code paths, one is specifically Pentium4 SIMD optimization, one is optimized for other CPUs. The specific code run path automatically selection at runtime. (Through analysis of different C / C compiler performance, I think the Inter compiler is very good for his own chip optimization, and the code performance compiled for Digital Mars can represent the general C / C performance)

The comparison of these language comparisons is not practical because some specific functions are not implemented in these languages. For example, the C built-in library has no (string-specific symbol statistics) function. However, since this article's theme is to discuss C #, the test is made of C # support. If other languages ​​do not provide the corresponding function, we will provide a custom implementation. At the same time, if a language is very poorly supported by a certain function, I will consider providing a relatively good custom support. (Especially for C / C , at least one or two places can see the real performance of the language itself)

In short, I will not list each test in different languages, but here, I will introduce the following tests in detail:

NOOP.NOOP Test is the loading time of the program: an empty main / main execution time. Different from other tests, this test is done under the PTIME tool. (You can get from http://synesis.com.au/r_systools.html), it provides a function similar to the Time tool in the UNIX environment, which You can also run the target program multiple times to get the average (in our test, C / C / D run 200 times, C # run 50 times, Java run 30 times). In order to remove certain specific circumstances, we will Multiple measurements (Translator Note: Refers to the multi-run PTIME program, the PTIME program itself runs more running the target program) and takes the average of the highest value and the lowest value.

F2i This test converts the double precision floating point value of 1 length to 32bit to 32bit. As can be seen from Listing 1, the use of pseudo-random number generation algorithms can ensure that each language is more floating point number. This brings two interesting situations, we will discuss it when viewing results.

The I2F is the opposite of the previous one, which is tested for a specific function. The program generates 10,000 determined pseudo-random integers to ensure that the integers converted in different languages ​​are the same.

I2STR converts integers into a string type. Integer variables increase from 0 cycles to 5000000, each time you do an integer to string-type conversion (see Listing2) .C # / java with built-in functions --- i.toString () and Integer.toString (i) C / D is done with traditional sprintf (). C is divided into twice. The first time ISTREAM's strStream class, like expected, in this case is very poor. In order to improve efficiency, I have taken some measures, so I2Str2Str2i. Convert the string into integers. Create a string array, the content is the content of I2STR converted, and then convert the string back through the language itself or the library function, calculate the time (see Listing3). C / D is completed with ATOI, C with iostream STRSTREAM is complete, C # is done with int32.parse, and Java is complete with integer.parseint ().

Picalc iteration and floating point. The percentage is calculated by the iterative algorithm shown by Listing 4. Each language is implemented with its iteration is 10,000,000.

转载请注明原文地址:https://www.9cbs.com/read-15529.html

New Post(0)