在调试模式下销毁协议缓冲区消息几乎比在释放模式下慢500倍

时间:2012-08-07 23:12:50

标签: c++ performance destructor protocol-buffers debug-mode

在调用中使用以下代码和您自己的定时代码来删除main()中的msg。在调试模式下运行时,平均需要473倍,而不需要调试。 有谁知道为什么会这样?如果是这样,有没有办法让我的代码在调试模式下运行得更快?

注意:我在Windows 7计算机上使用Visual Studio 2008 SP 1.

// This file is generated by using the Google Protocol Buffers compiler
// to compile a PropMsg.proto file (contents of that file are listed below)
#include "PropMsg.pb.h"

void RawSerializer::serialize(int i_val, PropMsg * o_msg)
{
        o_msg->set_v_int32(i_val);
}
void serialize(std::vector<int> const & i_val, PropMsg * o_msg)
{
    for (std::vector<int>::const_iterator it = i_val.begin(); it != i_val.end(); ++it) {
        PropMsg * objMsg = o_msg->add_v_var_repeated();
        serialize( * it, objMsg);
    }
}

int main()
{
    std::vector<int> testVec(100000);
    PropMsg * msg = new PropMsg;
    serialize(testVec, msg);
    delete msg; // Time this guy
}

使用以下.proto文件定义创建PropMsg:

option optimize_for = SPEED;
message PropMsg
{
  optional int32 v_int32 = 7;
  repeated PropMsg v_var_repeated = 101;
}

这是我得到的一些示例测试输出:

datatype: class std::vector<int,class std::allocator<int> >
                               num runs:                   10
                              num items:               100000
        deserializing from PropMsg time:               0.0046
            serializing to PropMsg time:               0.0426
                 reading from disk time:               0.7195
                   writing to disk time:               0.0298
              deallocating PropMsg time:                 8.99

注意这不是IO绑定的。

1 个答案:

答案 0 :(得分:1)

VS Debug中的STL容器非常慢。游戏程序员论坛充斥着对此的抱怨。人们常常选择其他实施方案但是,根据我的阅读,您可以通过禁用迭代器调试/检查来提前提升性能:

#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0

可能影响调试性能的其他因素是对new和delete的过度调用。内存池可以帮助解决这个问题。您尚未提供PropMsg::add_v_var_repeated()PropMsg::~PropMsg()的详细信息,因此我无法发表评论。但我假设该类中有一个向量或其他STL容器?