最近,我已经开始使用Visual 2013用 Server测试OTL库。我的测试表明,针对10000计数表的简单select语句的性能比类似的.NET 4.0测试应用程序的性能慢40%。所有测试都是在打开两个平台的所有优化时执行的。
这两个应用程序都执行以下任务:为容器对象打开db连接创建(和预留空间)。执行select语句命令。对于从db获取的每条记录,使用db(stream/reader)对象创建一个实体,将对象添加到容器关闭
.NET C#应用程序需要0.5秒才能完成这项任务,而OTL++应用程序需要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;
}发布于 2015-03-25 12:08:13
我不这么认为,当您查看otl的代码源时,它确实对Server使用了ODBC,并且只是作为odbc顶层进行了优化。出于性能原因,Server .NET 4.0将使用Server而不是odbc。
另外,如果不预先分配内存消耗,则由于.NET函数调用,您将始终松散到SysAllocMem和Java。这就像尝试测量对SysAlloc的4000次调用和对SysAlloc的1次调用。性能问题与这些功能直接相关。
https://stackoverflow.com/questions/24031312
复制相似问题