ブースト マルチアレイのパフォーマンスが STL Vector に比べて非常に悪いことに気付きました。以前に尋ねられたこの質問に出くわしました。最も気に入った回答は、
1) ブーストはネイティブ アレイとほぼ同じ速度です
2) Boost MultiArray から最高のパフォーマンスを得るには、データ要素にアクセスする順序を変更する必要があります。また、デバッグではなく、リリース モードで実行する必要があります。
まあ、私はそれをすべてやりましたが、それでも私の MultiArray のパフォーマンスはかなりお粗末です。
ここにコードを投稿しています:
A) デフォルトの順序で
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
#include <stdio.h>
#include <conio.h>
#include <iostream>
int main(int argc, char* argv[])
{
const int X_SIZE = 400;
const int Y_SIZE = 400;
const int ITERATIONS = 500;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
// Create the native array
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
//------------------Measure boost----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE; ++y)
{
for (int x = 0; x < X_SIZE; ++x)
{
boostMatrix[x][y] *= 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Boost] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
//------------------Measure native-----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE; ++y)
{
for (int x = 0; x < X_SIZE; ++x)
{
nativeMatrix[x + (y * X_SIZE)] *= 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Native]Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
return 0;
}
B) 逆順の場合
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
#include <stdio.h>
#include <conio.h>
#include <iostream>
int main(int argc, char* argv[])
{
const int X_SIZE = 400;
const int Y_SIZE = 400;
const int ITERATIONS = 500;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
// Create the native array
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
//------------------Measure boost----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
boostMatrix[x][y] *= 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Boost] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
//------------------Measure native-----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
nativeMatrix[x + (y * X_SIZE)] *= 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Native]Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
return 0;
}
すべての可能な順列で、私のベンチマークはほぼ同じです:
1) ネイティブ コードの場合: 0.15 秒
2) Boost MultiArray の場合: 1.0s
Visual Studio 2010 を使用しています。
私の質問は: Visual Studio を使用している場合、Boost MultiArrays から優れたパフォーマンスを得るにはどうすればよいですか?
アップデート :
Visual Studio 2013 に切り替えました。そこで、Qvec-report2 コンパイラ スイッチを有効にしました。そして非常に興味深いことに、コンパイルすると、コンパイラーがベクトル化に失敗したという情報メッセージを受け取り始めました。以下は、ほとんど警告のように見える情報メッセージの例です。最も単純なコードに対して、そのようなメッセージをいくつか受け取りました。
--- 解析関数: void __cdecl `vector constructor iterator'(void * __ptr64,unsigned __int64,int,void * __ptr64 (__cdecl*)(void * __ptr64)) 1> D:\Workspace\test\Scrap\Scrap\Source .cpp : 情報 C5002: ループは理由 '1301' によりベクトル化されませんでした
これは、Boost マルチアレイが GCC では問題なく動作するのに、Visual Studio では動作が遅い理由についての主要な手がかりだと思います。この追加情報を踏まえて、問題を解決する方法を考えていただけますか?
@Admins : 以前に回答した質問のマークを外してください。大幅な編集を行いました。