C # and C ++ speed competition (2)

zhaozj2021-02-17  52

Data Inventory Test In this section, we will test both C and C # code to test both the access and processing of the database. The method is to operate the same database table. The surface structure is as follows:

Create Table TestTable

(

COL1 INTEGER,

Col2 Varchar (50),

PRIMARY Key (COL1)

)

Test will be divided into three parts, the first portion and the third part are concentrated on the data processing, and the second part is centralized to data access. The test results of data processing and data access are rendered separately. Here is the C code for data access and processing:

Code 5: db.cpp

#import "msado15.dll" /

NO_NAMESPACE RENAME ("EOF", "endoffile")

#include

#include

#include

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

{

IF (argc! = 2)

{

Std :: CERR << "USAGE: / TDB [ROWS] / N";

Return 1;

}

:: Coinitialize (NULL);

INT NUM = ATOI (Argv [1]);

DWORD DW = :: gettickcount ();

_ConnectionPtr Conptr (__ uuidof (connection));

Conptr-> Open (l "provider = microsoft.jet.Oledb.4.0;"

"DATA SOURCE = C: //db.mdb;",

L ""

L ""

AdopenunSpecified;

For (int i = 0; i

Execute (SQL, & Recordseffected, AdcmdText);

}

DWORD DW2 = :: gettickcount ();

Std :: cout << "MilliseConds =" << DW2-DW

<< std :: endl;

DW = :: gettickcount ();

For (int J = 0; j <100; j )

{

_RecordSetPtr Rsptr (__ uuidof (recordset);

Rsptr-> Open (L "SELECT COL1, Col2 from TestTable",

Conptr.getInterfacePtr (),

AdopenForwardonly, AdlockOptimistic, AdcmdText);

While (Rsptr-> Endoffile)

{

_VARIANT_T V1 = Rsptr-> getCollect ("col1");

_VARIANT_T V2 = Rsptr-> getCollect ("col2");

Rsptr-> moandenext ();

}

Rsptr-> close ();

}

DW2 = :: gettickcount ();

Std :: cout << "MilliseConds =" << DW2-DW

<< std :: endl;

DW = :: gettickcount ();

For (int i = 0; i

The table below is running ten times, each 100-line record

Table 3: Database test results

Serial number C (~ milliseconds) C # (~ ms) 11612/441/450 4086/630/5602391/410/441 490/440 480/510/4404371/420/451 470/510/4505370/421 / 461 460/500/4506371/420/461 470/400/471 470/500/46083/410/4709370/421/450 470/510/47010391/410/461 460/510 / 470 Average 499/419/454 832/531/475 This result is very surprised. The performance of .NET is satisfactory. Generally speaking, 25% of the performance can be endured. This shows that Net is here a winner.

XML performance test

XML is the latest technology in the field of data processing. Many people are very interested in processing with C # codes and Visaul C code or resolve XML files.

Here is a C code for access and processing XML:

Code seven: XML.cpp

#import named_guids

#include

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

{

IF (argc! = 2)

{

Std :: CERR << "USAGE: / TXML [FileName] / N";

Return 1;

}

:: Coinitialize (NULL);

DWORD DW = :: gettickcount ();

For (int i = 0; i <100; i )

{

Msxml2 :: ixmldomdocumentptr domdocument

MSXML2 :: CLSID_DOMDocument);

_BSTR_T filename = argv [1];

DomDocument-> async = false;

DomDocument-> load (filename);

}

DWORD DW2 = :: gettickcount ();

Std :: cout << "MilliseConds =" << DW2-DW

<< std :: endl;

:: Couninitialize ();

Return 0;

}

Here is a C # code for accessing and processing XML:

Code seven: XML.cs

Using system;

USING SYSTEM.XML;

Namespace XML2

{

Class class1

{

Static void main (string [] args)

{

IF (args.length! = 1)

{

Console.writeline ("Usage: / TXML [filename]");

Return;

}

Long dt = datetime.now.ticks;

For (int i = 0; i <100; i )

{

XmLDocument Doc = New XmLDocument ();

Doc.Load (Args [0]);

}

Long dt2 = datetime.now.ticks;

System.console.writeline ("MilliseConds = {0}", (DT2-DT) / 10000);

}

}

} The result of running ten times is as follows:

Table 4: XML test results

Serial number C (~ milliseconds) C # (~ ms) 1241 1111170 8413161 84170 861517081717170 8615170816171717170170851 Average value 203873 This result is surprising. It is difficult to believe that the running efficiency of the .NET XML class is from four to five times higher than the equivalent ActiveX class. Why do this happen? Maybe only Microsoft brothers know. Perhaps Microsoft wants to design the .NET class into a certain aspect. If this is not the case, then Microsoft's guy should optimize their .NET XML class.

If you are a developer of web services and server applications, when you use the .NET framework for a high-performance app, you should think twice, especially the XML service.

to sum up

It is necessary to emphasize the .NET framework or a new technology. Therefore, there are still many things you need in this frame, it also needs to be continuously optimized. In addition, the performance test of .NET is also very superficial, with the rich connotation of .NET, with the above four aspects of testing and simple articles to explain its fast or slowness is far less than enough.

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

New Post(0)