在调用 main() 中删除消息时,将以下代码与您自己的计时代码结合使用。在 Debug模式下运行时,平均花费的时间是未 Debug模式下运行时的 473 倍。 有谁知道为什么会这样?如果是这样,有没有办法让这段代码在 Debug模式下运行得更快?
注意:我在 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
}
PropMsg 是使用以下 .proto 文件定义创建的:
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 绑定(bind)的。
最佳答案
众所周知,VS 调试中的 STL 容器速度很慢。游戏程序员论坛充斥着对此的提示。人们通常会选择替代实现。但是,根据我的阅读,您可以通过禁用迭代器调试/检查来预先提高性能:
#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0
其他可能影响调试性能的因素是对 new 和 delete 的过多调用。内存池可以帮助解决这个问题。您尚未提供 PropMsg::add_v_var_repeated()
或 PropMsg::~PropMsg()
的详细信息,因此我无法发表评论。但我假设该类中有一个 vector 或其他 STL 容器?
关于c++ - 在 Debug模式下销毁 Protocol Buffer 消息比在 Release模式下慢近 500 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11855409/