【发布时间】:2014-06-04 07:16:32
【问题描述】:
我最近开始使用 Visual Studio 2013 在 SQL Server 上测试 OTL 库。我的测试表明,针对 10000 个计数表的简单选择语句的性能比 类似的 .NET 4.0 测试应用程序的性能慢 40%。所有测试都是在为两个平台开启所有优化的情况下执行的。
这两个应用都执行以下任务: 打开数据库连接 为容器对象创建(并保留空间)。 执行选择语句命令。 对于从 db 获取的每条记录 使用 db(stream/reader) 对象创建实体 将对象添加到容器 关闭
.NET C# 应用程序需要 0.5 秒才能完成这项任务,而 OTL-C++ 应用程序需要 0.7 秒才能完成,我想知道是否可以优化 C++ 应用程序以更快地执行? p>
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;
}
【问题讨论】:
-
我使用 OTL 与 DB2 的 ODBC 连接,性能没有任何问题。
标签: visual-c++ otl