C # and C ++ speed competition (first part)

zhaozj2021-02-17  52

C # is currently the application development language of Microsoft .NET platform first. The application written by C # must run in a special environment, the control environment (Managed). What is the performance of the C # application compared to C of unmanaged (unmanaged)? After several performance tests, we found that the C # app runs far less than non-controlled C applications. In this speed of this speed, non-controlled C has a significant advantage. It will continue to be the favorite of most programmers.

This paper is proposed to test C # and C programs through some orthodox and non-orthodox performance test methods. In the statistics of these tests. The reader will find the performance of the C # program. As a programmer, I have a unique momentum for C . In my career, I have been using Visual C . I hope this article will give programmers a little prompt to select the appropriate programming language when writing different applications.

Visual C is one of the best compilers on the Win32 platform, I think this has become a consensus. .NET is a new application platform provided by Microsoft.

First of all, I explain the hardware and software environment of your machine:

Hardware environment: Dell Inspiron 3800 G700GT laptop CPU / PIII / 700, ROM / 128MB, HD / 12GB.

Software Environment: Windows 2000 SP2, .NET Platform Visual Studio.net, Office XP.

All tests are compiled in the Release mode compiler in the command line, not the Visual Studio IDE integration development environment, and execute the program in the command line state. There is no optimization for the compilation process.

The tests of this paper consist of four parts. These include tests and other single tests for the famous Euters of Eratos of Erasthenes, and the main test of a single test. The performance of a specific project in the .NET framework:

Hello World Tests a problem with the Net Framework is the startup time of the program. Because the .NET framework is above Win32, you want to start a .NET framework requires additional startup time overhead. The Eratovi Eutosthenes Tests this filtering algorithm is an ancient way to find the number of prime. Because development of this algorithm is used for humans, this algorithm takes up the CPU resource (non-CPU optimization) to provide a very good reference reflection. Remember, writing a program is always a person. Data Instrument Test Today's application server often uses the database, so I think that using ADO.NET to test C #'s data inventory performance and data inventory with regular ADO test Visual C is compared to the problem. A good way. XML Test XML is the latest and popular technologies. So many people will be interested in the performance of XML with C # and Visual C . This article does not intend to explain the test results. In each test, I first listed algorithms and code used for testing. Then list the test results data. Finally, a brief conclusion is submitted for these data.

One thing is quite important in the test process, that is, I tried to make the test code of the two environments as the same as possible. This makes the test results more convincing.

Hello World

The Hello World Test program is mainly to evaluate the time used in loading a program and its runtime environment. The C program is running requires a C run library, which is well known, this library is quite lightweight. The operation of the C # program must be loaded .NET framework, from the current situation, this framework is not a lightweight. The Hello World program C code is as follows:

Code 1: HelloWorld.cpp

#include

Int main (int Argc, char * argv [])

{

Std :: cout << "Hello World" << std :: endl;

Return 0;

}; The C # code of the Hello World program is as follows:

Code 2: HelloWorld2.cs

Using system;

Namespace HelloWorld

{

Class class1

{

Static void main (string [] args)

{

Console.writeline ("Hello World");

}

}

} From the results of this test, we can see the load time of the program in the corresponding environment. For a program that only completes the simple task, it is undoubted that we need it to quickly load and exit. The Perl script is a representative example that runs this requires a large amount of load time, resulting in the need to meet the needs of CGI and performance-oriented Web sites. At this time, people often choose C program for Perl scripts. But for programs that need to be activated for a long time, its loading time is not so important to relatively time performance. The following table is the result of ten tests:

Table 1: Hello World Test Results

Serial number C (~ milliseconds) C # (~ ms) 140 1221220121310 130410 100510 110610 130710 120810 140910 1501020 140 average value 15 235

The accuracy of the test results is determined by the accuracy of the GetTickCount function. Its precision is probably one percent. From the result we can draw this conclusion. First, cold start. The time spent in the NET application process is more than one second more than the same application. Second, when the program started, the C code is approximately one-tenth of the C # code runtime overhead. Generally, this difference can be ignored. Erat Toyi Filter Algorithm Tests the Erat Toyi Filter Algorithm Test Program Evaluate Basic Integer Algorithm and Compare Logic. This algorithm has a long history, it already exists before the computer appears. So use it to assess the algorithm performance created in various environments has certain typical or representative. The C code of the Eratoyi filter algorithm is as follows:

Code 3: sieve.cpp

#include

#include

#include

#include

#include

Using namespace std;

Int main (int Argc, char * argv [])

{

IF (argc! = 2)

{

Std :: CERR << "USAGE: / TSIEVE [ITERATIONS] / N";

Return 1;

}

SIZE_T NUM = ATOI (Argv [1]);

DWORD DW = :: gettickcount ();

Vector

PRIMES (8192 1);

Vector

:: Iterator Pbegin = Primes.Begin (); Vector

:: item begin = pbegin 2;

Vector

:: items.erator end = primes.und ();

While (NUM -)

{

Fill (Begin, End, 1);

FOR (Vector

:: Iterator i = begin;

I

{

IF (* i)

{

Const size_t p = i - pbegin;

FOR (Vector

:: Iterator K = i P; K

Here is the C # code of the Erat Toyi Filter Algorithm:

Code 4:

Using system;

Namespace Sieve

{

Class class1

{

Static void main (string [] args)

{

IF (args.length! = 1)

{

Console.writeline ("Usage: / Tsieve"

"[ITerations]");

Return;

}

Int num = int.parse (args [0]);

Long dt = datetime.now.ticks;

int [] primes = new int [8192 1];

INT PBEGIN = 0;

INT begin = 2;

INT end = 8193;

While (NUM -! = 0)

{

For (int i = 0; i

{

Primes [i] = 1;

}

For (int i = begin; i

{

IF (Primes [I]! = 0)

{

INT P = I - Pbegin;

For (int K = i p; k

{

Primes [k] = 0;

}

}

}

}

Long dt2 = datetime.now.ticks;

System.console.writeline ("MilliseConds = {0}",

(DT2-DT) / 10000);

}

}

} The result of the test is not enough to indicate the environment faster. In these two language tests, I aim to explain which language constructs have the greatest impact on the results of the test. When you choose a language based on performance, you should directly consider which type of performance needs to be considered. Here, the Erat Toyi Filter Algorithm is tested by the loop structure and the process of comparing logic and integer. Here are ten tests, each time a 10,000-time repetition test results:

Table 2: Filter algorithm test results

Serial number C (~ milliseconds) C # (~ ms) 11342 27242134227142 272422441342 27245172 27342 2722 2727362 273481352 27362 2724101352 2724 average value 13482726

This result can explain the problem. Integer calculating C # spent twice the time of C . So for a logical complex server, use non-managed C code than C # code is more suitable. There is a difference between the C code and the C # code, ie the C # is used by the unit, and the C code is used by the vector template class. I rewrite the C code with the array of natives, and should be faster. The result is not the case, the native C array is performed in 1900 milliseconds. (to be continued)

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

New Post(0)