2

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バウンドではないことに注意してください。

4

1 に答える 1

1

VSデバッグのSTLコンテナは、低速であることがよく知られています。ゲームプログラマーのフォーラムは、これについての不満でいっぱいです。多くの場合、人々は代替の実装を選択します。ただし、私が読んだことから、イテレータのデバッグ/チェックを無効にすることで、パフォーマンスを事前に向上させることができます。

#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0

デバッグのパフォーマンスに影響を与える可能性のある他のことは、newとdeleteの過度の呼び出しです。メモリプールはそれを助けることができます。PropMsg::add_v_var_repeated()またはの詳細を提供していないPropMsg::~PropMsg()ため、コメントできません。しかし、そのクラス内にベクターまたは他のSTLコンテナーがあると思いますか?

于 2012-08-07T23:33:19.417 に答える