PDA

View Full Version : QVector Slower than STL Vector



lynchkp
4th December 2010, 00:05
Hey guys,

I have a question regarding QVector. I switched from using STL vectors throughout a portion of my code to using QVector since the documentation mentioned it as being a more lightweight and faster container. However, when I performed the switch, I noticed about a 25% slowdown in my cross-correlation code performance. I've attached a sample bit of code which is the bottleneck. I worked pretty hard optimizing it with STL vectors, but I am unsure if due to implicit data sharing or other features of QVector, there may be an even faster way to do it. Thanks!

(For example, this code takes around 4800 msec to run, versus 3200 msec for STL Vector).



void corr_dcc(
const QVector< QVector<quint16> >& IA,
const QVector< QVector<quint16> >& IB,
QVector< QVector<qreal> >& map)
{
quint16 i, j, x, y;
quint16 wind_xsize = IA.size();
quint16 wind_ysize = IA.at(0).size();
quint16 xshift = IB.size() - wind_xsize;
quint16 yshift = IB.at(0).size() - wind_ysize;

for(x = 0; x < xshift; x++) {
for(i = 0; i < wind_xsize; i++) {
const quint16* IA_i = &IA.at(i).at(0);
const quint16* IB_ix = &IB.at(i + x).at(0);
for(y = 0; y < yshift; y++) {
qreal& map_yx = map[x][y];
for(j = 0; j < wind_ysize; j++) {
map_yx += ((qreal) IA_i[j]) * IB_ix[j + y];
}
}
}
}
}


Thanks!!

SixDegrees
4th December 2010, 09:57
If performance is the primary concern, you should use plain old pointers instead of more complex objects. The latter always have additional overhead associated with them.

I'm not sure why you're using 16-bit ints, either. These short integers may not perform as well as whatever int corresponds to on a particular platform, although this isn't a hard and fast rule.

Finally, correlation routines are often faster in the Fourier domain if the arrays involved are large. You have to experiment to find the break-even point, but this can often yield a significant speedup.

lynchkp
5th December 2010, 10:05
Thanks for the reply,

I'm aware of FFT correlation algorithms, however I'd like to get this algorithm working first because it is faster for small sample areas.

What would be an example of writing that code using simpler objects? I tried using pointers in the most intensive parts of the code. Still, I find it interesting that simply changing from std::vector to QVector (the only change!) caused a 25% slowdown.

Thanks for the tip about 16-bit ints. I wasn't aware of that, but I'll try it and see if it speeds up. Thanks!

squidge
5th December 2010, 10:50
Did you do these speed tests in debug or release mode? Debug mode is likely to be slower for QVector.