画像処理の高速化・番外編

昨日のEffectiveC++読書会#4で、
動的に確保する配列データもdeleteを使わんですむように、vector使えばいいじゃん。
vector使ってもパフォーマンスは変わらないよ。という話があがったので

それじゃってことで、やってみました。

int channel;
int width;
int height;
std::vector data(width * height * channel);

for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++ )
{
for ( int c = 0; c < channel; c++ )
{
data[y * width * channel + x * channel + c] = F(data[y * width * channel + x * channel + c]);
}
}
}

ちなみに、F(x)は、明るさ・コントラスト調整のトーンカーブ関数です。
画像データサイズは、1252x1252の32bitの、約6.2MB。

結果。
2612ms!! おっそ

次に、イテレータで回してみました。

std::vector::iterator it = data.begin();

for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++ )
{
for ( int c = 0; c < channel; c++, ++it )
{
*it = F(*it);
}
}
}

893ms。 うん、さっきよりはまし。

ちなみに、これを配列データで行った場合、
unsigned char* data = new unsigned char[width * height * channel];

30ms。

計測は、あくまでデータアクセスの箇所(for文三重ループのところ)のみに対して行いました。

てことで、vectorはゼロオーバーヘッドではないってことでいいのかなぁ。