分类:
2008-10-14 14:57:58
C#和C++的速度大比拼(第二部分)
编译:
CREATE TABLE testtable ( col1 INTEGER, col2 VARCHAR(50), PRIMARY KEY (col1) )
代码五: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->MoveNext(); }; rsptr->Close(); }; dw2 = ::GetTickCount(); std::cout << "Milliseconds = " << dw2-dw << std::endl; dw = ::GetTickCount(); for (int i=0;i 下表为运行十次,每次100行记录的结果 表三:数据库测试结果
序号 |
C++(~毫秒) |
C#(~毫秒) |
1 | 1612/441/450 | 4086/630/560 |
2 | 391/410/441 | 490/630/520 |
3 | 370/421/440 | 480/510/440 |
4 | 371/420/451 | 470/510/450 |
5 | 370/421/461 | 460/500/450 |
6 | 371/420/461 | 470/500/460 |
7 | 370/411/471 | 470/500/460 |
8 | 381/410/451 | 460/510/470 |
9 | 370/421/450 | 470/510/470 |
10 | 391/410/461 | 460/510/470 |
平均值 | 499/419/454 | 832/531/475 |
代码七:xml.cpp #import下面是一段存取和处理XML的C#代码: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; }
代码七: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); } } }运行十次的结果如下:
序号 | C++(~毫秒) | C#(~毫秒) |
1 | 241 | 1111 |
2 | 170 | 841 |
3 | 161 | 841 |
4 | 170 | 861 |
5 | 160 | 861 |
6 | 171 | 851 |
7 | 170 | 841 |
8 | 160 | 831 |
9 | 160 | 841 |
10 | 170 | 851 |
平均值 | 203 | 873 |