QCustomPlot Discussion and Comments

Preferred way to add QCPGraph data from std::vectorReturn to overview

Hi,

I am enjoying my new found gift (at the right time of the year) that is QCustomPlot. Usually I have large sets of data either in the form of std::vector or const double * data pointers. I have implemented the following method and it works just fine:

QVector<double> qxVals = QVector<double>::fromStdVector(xVals);
QVector<double> qyVals = QVector<double>::fromStdVector(yVals);
qcpGraph->setData(qxVals, qyVals, true); // already sorted

There is memory and cost associated with conversion from std::vector to QVector, then QCPGraph internally converts it to a list of QCPGraphData. I am calling this method 1.

I see that there are 2 other ways we can do this. This is Method 2 (pseudo-code to keep it short):

QVector<QCPGraphData> qcpvGraphData = ::fromStdVectors(xVals, yVals);
qcpSharedPointerGraphDataContainer->set (qcpvGraphData, true);
qcpGraph->setData(qcpSharedPointerGraphDataContainer, true);

Method 3 (again pseudo-code, let's not worry about the syntax):

for_each (xVal, yVal) in (xVals, yVals) {
  qcpSharedPointerGraphDataContainer->add(QCPGraphData{xVal, yVal});
  // does add implement emplace / std::move idioms?
}
qcpGraph->setData(qcpSharedPointerGraphDataContainer, true);

Before I spent time coding methods 2 and 3 in order to assess the relative performance, I thought I would find out what is the recommended practice for these large data sets.

Clearly in Methods 1 and 2, there is an intermediate copy of QVector<T> which gets copied into QCPGraphDataContainer eventually. But in Method 3, sorting has to be done in the add method. Of course it would be nice to have add(QCPGraphData&&, bool) to gain some performance.

Any thoughts would be helpful. Thanks.

generally speaking when programming, you use whatever structure you need for whatever you need it for. if i need to use a qvector for something and have no real reason to use a std vector, i will use qvector for everything.

as far as the question goes, i'm pretty sure the best method for this is to use the QCPGraphDataContainer as your actual data container, and then pass in the pointer to the graph. This mostly depends on how often you have to set/add to the data. if you just have one large dataset that you add and then never add again, it probably doesnt matter at all.

My use case is realtime data, so i have to add to the plot 20ish times per second, so it would get really awful to use the set method with a vector because it would be redoing work over and over again. in my case the fastest method is just to store the data in the form that qcustomplot wants and just add to the data.