main()でmsgを削除するには、呼び出しの前後に独自のタイミングコードを指定して次のコードを使用します。デバッグモードで実行する場合、デバッグなしで実行する場合の平均473倍の時間がかかります。なぜこれが起こっているのか誰かが知っていますか?もしそうなら、このコードをデバッグモードではるかに高速に実行させる方法はありますか?
注:Windows7マシンでVisualStudio 2008SP1を使用しています。
// 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バウンドではないことに注意してください。