我最近开始使用 sql server 使用 Visual Studio 2013 使用 sql server 测试 Otl库。我的测试表明,对10000计数表的简单选择语句的性能比相似的.NET 4.0 测试应用程序的性能慢40%。所有测试都进行了所有测试,所有优化对于两个平台打开。

两个应用程序执行以下任务: 打开DB连接 为容器对象创建(和保留空间)。 执行SELECT语句命令。 对于从DB获取的每个记录 使用DB(Stream / Reader)对象创建实体 将对象添加到容器 关闭

.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;
}
.

有帮助吗?

解决方案

我不这么认为,当您查看OTL的代码源时,它会为SQL Server使用ODBC API,仅作为ODBC顶层优化。SQL Server .NET 4.0将使用SQL驱动程序API而不是ODBC API进行性能原因。

此外,如果您不重新分配您的内存消耗,则由于Sysallocmem函数调用,您将始终松开.NET和Java。这就像尝试测量Sysalloc vs 1调用sysalloc的4000次呼叫。您的性能问题与这些功能直接相关联。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top