最近、 Visual Studio 2013を使用して、SQL ServerでOTL ライブラリのテストを開始しました。私のテストでは、10000 カウント テーブルに対する単純な select ステートメントのパフォーマンスが、同様の .NET 4.0テスト アプリケーションのパフォーマンスよりも 40% 遅いことが示されました。すべてのテストは、両方のプラットフォームですべての最適化をオンにして実行されました。
どちらのアプリも次のタスクを実行します。 データベース接続を開きます コンテナー オブジェクトの作成 (および領域の予約) select ステートメント コマンドを実行します。db からフェッチされた各レコードに対して、db(stream/reader) オブジェクトを使用してエンティティを作成します オブジェクトをコンテナに追加します close
.NET C# アプリはこのタスクを完了するのに 0.5 秒かかりますが、OTL-C++ アプリは完了するのに 0.7 秒かかります。C++ アプリを最適化してより高速に実行することは可能でしょうか?
C++ コードのスニペット:
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
#define OTL_CPP_11_ON
#define OTL_STL // Turn on STL features
#define OTL_ANSI_CPP // Turn on ANSI C++ typecasts
#define OTL_UNICODE // Enable Unicode OTL for ODBC
#include "otlv4.h"
class Employee
{
private:
int employeeId;
wstring regno;
wstring name;
wstring surname;
public:
Employee()
{
}
Employee(otl_stream& stream)
{
unsigned short _regno[32];
unsigned short _name[32];
unsigned short _surname[32];
if (!stream.is_null())
{
stream >> employeeId;
}
if (!stream.is_null())
{
stream >> (unsigned char*)_regno;
regno = (wchar_t*)_regno;
}
if (!stream.is_null()){
stream >> (unsigned char*)_name;
name = (wchar_t*)_name;
}
if (!stream.is_null()){
stream >> (unsigned char*)_surname;
surname = (wchar_t*)_surname;
}
}
int GetEmployeeId() const
{
return employeeId;
}
};
otl_connect db;
int main()
{
otl_connect::otl_initialize();
try
{
otl_connect::otl_initialize();
try
{
// connect
db.rlogon("DSN=SQLODBC");
// start timing
clock_t begin = clock();
otl_stream i(10000, "SELECT Id, Field1, Field2, Field3 FROM Test", db);
// create container
vector<Employee> employeeList;
employeeList.reserve(10000);
// iterate and fill container
while (!i.eof())
{
Employee employee(i);
employeeList.push_back(employee);
}
i.close();
// cleanup
size_t size = employeeList.size();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Total records:" << size << endl;
cout << "Time elapsed to read all records:" << elapsed_secs << endl;
}
catch (otl_exception& p){ // intercept OTL exceptions
cerr << p.msg << endl; // print out error message
cerr << p.stm_text << endl; // print out SQL that caused the error
cerr << p.sqlstate << endl; // print out SQLSTATE message
cerr << p.var_info << endl; // print out the variable that caused the error
}
db.logoff();
return EXIT_SUCCESS;
}