I have a code base that plots many scope-like curves using QT library. We are trying to optimize the running time of this application (because we reached the wall on CPU usage). After some profiling I found that the main data plot loop calls this function from QWT library:
void QwtPlotCurve::setSamples( const QVector<double> &xData, const QVector<double> &yData )
{
setData( new QwtPointArrayData( xData, yData ) );
}
This is done every 100ms. As a result, every 100ms there is a new
call for QwtPointArrayData
object, then there is a delete
call for the previous version of the QwtPointArrayData
object, for every curve on every scope.
My question is: is there a better more efficient way to plot data without re-allocating it on every update?
Memory allocation can cause a severe bottleneck in any program, depending on how often it is called. It is a very expensive instruction in terms of performance and one should always desing an architecture, such that this doesn't occur.
There are many better ways, you absolutely don't need to re-allocate new memory for QwtPointArrayData
. Take a look at the examples of qwt library, such as oscilloscope or realtime. Basically you can use a QwtDirectPainter
and its function drawSeries
, which requires as arguments a QPlotCurve and the number of samples you wish to draw.
https://qwt.sourceforge.io/class_qwt_plot_direct_painter.html
Of course you have to set the samples of the curve through setSamples() or setRawSamples instruction
https://qwt.sourceforge.io/class_qwt_plot_curve.html#aa51cd3fa00f2a046ca5a9889c5db2413
This is a very efficient approach. But again, go to the folder examples of the library to find oscilloscope and realtime examples, and study the library itself, as there are might be other approaches that could suit you better.
EDIT: I didn't understand at first that this was a code snippet from the library. I think you should try setRawSamples instead, as it should be more efficient https://qwt.sourceforge.io/class_qwt_plot_curve.html#afd13c94e23520dacbc37b4d0fd036a8b