3

独自のベクター クラスの実装を開始したばかりで、単純なファイルでテストして、完了するまでの時間を確認しています。1 つのテストには 2:30 分かかり、他のテストには 90 秒と 29 秒かかりました。

このクラスのパフォーマンスに何かが当たっています。ソースを追跡するのを手伝ってもらえますか?

テスト:

#include "MyVector.h"

const unsigned int SIZE_V= 1000000;
const unsigned int RUNS= 10000;

int main() {

      MyVector v(SIZE_V);

      for (unsigned int j=0; j<RUNS; ++j) {
        for (unsigned int i=0; i<SIZE_V; ++i) {
          v[i]= i;
        }
      }

      return 0;
}

クラス:

MyVector.h:

#ifndef MY_VECTOR_H
#define MY_VECTOR_H

class MyVector {

 public:

      MyVector(unsigned int size);
      ~MyVector();

      int& operator[](unsigned int i);

 private:
      int* _data;
      unsigned int _size;
      MyVector(const MyVector&);
      MyVector& operator=(const MyVector&);

};
#endif

MyVector.cpp:

#include "MyVector.h"
#include <assert.h>

MyVector::MyVector(unsigned int size) : _data(new int[size]) {
}

MyVector::~MyVector() {
      delete[] _data;
}

int& MyVector::operator[](unsigned int i) {
      assert(i<_size);
      return _data[i];
}

編集:

テスト結果は次のとおりです。

granularity: each sample hit covers 4 byte(s) for 0.04% of 27.09 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0   12.51   14.58                 main [1]
               11.28    0.00 1410065408/1410065408     MyVector::operator[](unsigned int) [2]
                3.31    0.00       1/1           MyVector::~MyVector() [3]
                0.00    0.00       1/1           MyVector::MyVector(unsigned int) [7]
-----------------------------------------------
               11.28    0.00 1410065408/1410065408     main [1]
[2]     41.6   11.28    0.00 1410065408         MyVector::operator[](unsigned int) [2]
-----------------------------------------------
                3.31    0.00       1/1           main [1]
[3]     12.2    3.31    0.00       1         MyVector::~MyVector() [3]
-----------------------------------------------
                0.00    0.00       1/1           main [1]
[7]      0.0    0.00    0.00       1         MyVector::MyVector(unsigned int) [7]
-----------------------------------------------
4

3 に答える 3

2

あなたがやりたいことの1つは、operator[]インラインにすることです。これを行うと、私のボックスでのコードのパフォーマンスが 3 倍向上します。

real    0m18.270s

real    0m6.030s

後者のテストでは、テスト ループの各反復に約 0.6ns (!) または約 1.5 クロック サイクルかかります。

これは、g++ 4.7.2 with を使用する Sandy Bridge ボックスにあります-O3

PS コードにバグがあります。コンストラクターが初期化されない_sizeため、assert()未定義の動作があります。

于 2013-01-24T09:52:59.460 に答える
1
  1. プロファイラーを実行せずに測定します。

  2. 完全に最適化されたコードを測定します。g++ -O3

于 2013-01-24T10:00:51.220 に答える
0

あなたは書いている:-

1000000 * 10000 * 4 * 8 = 320000000000

次のようなテストでは、合計でデータのビット:-

2.5 mins = 2133333333 bits / sec = ~2,000 MB/s

90 secs = 3555555555 bits / sec = ~3,400 MB/s

30 secs = 10666666666 bits / sec = ~10,000 MB/s

DDR2のピークデータレートは3,200MB/sから8,533MB/ sで、DDR3のピークデータ範囲は6,400 MB / s〜17,066 MB /s/です。

それに基づいて、私はあなたがDDR3-1600RAMチップを持っていると思います。

于 2013-01-24T09:40:45.403 に答える