Results 1 to 5 of 5

Thread: QwtPlotCurve plots quite slowly with 5K points

  1. #1
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QwtPlotCurve plots quite slowly with 5K points

    Hi, I am using Qwt5 and Qt4.2.3 to plot curve with 5K points and it may take 20 seconds to plot every time.
    I looked up some similar threads in the forum but still have no idea.
    My code referred to Qwt Example realtime_plot:
    Qt Code:
    1. class CustomData
    2. {
    3. public:
    4. CustomData();
    5. void append(double *x, double *y, int count);
    6.  
    7. int count() const;
    8. int size() const;
    9. const double *x() const;
    10. const double *y() const;
    11. private:
    12. int d_count;
    13. QwtArray<double> d_x;
    14. QwtArray<double> d_y;
    15. };
    16.  
    17. //class CustomQwtPlot:public QwtPlot
    18. void CustomQwtPlot::appendDataShow(double *x, double *y, int size, const QPen &curvePen)
    19. {
    20. ......
    21.  
    22. if ( d_data == NULL )
    23. d_data = new CustomData;
    24. if(cur == NULL)
    25. {
    26. cur = new QwtPlotCurve("curve");
    27. cur->setPaintAttribute(QwtPlotCurve::PaintFiltered);
    28. cur->setRenderHint(QwtPlotItem::RenderAntialiased);
    29. d_data->append(x, y, size);
    30. cur->setRawData(d_data->x(), d_data->y(), d_data->count());
    31. cur->setPen(curvePen);
    32. cur->attach(this);
    33. }
    34.  
    35. ......
    36. }
    37.  
    38. //CustomQwtPlot *energyplot;
    39. void plot::updateEnergyplot(QList<QStringList> data, QStringList header)
    40. {
    41. ......
    42.  
    43. if (energyplot == NULL)
    44. {
    45. ......
    46. energyplot = new CustomQwtPlot;
    47. double *xData = new double[count];
    48. double *yData = new double[count];
    49.  
    50. for (int i = 0; i < count; i++)
    51. {
    52. xData[i] = (double)i;
    53. yData[i] = data.at(0).at(i).toDouble(&ok);
    54. }
    55. energyplot->appendDataShow(xData, yData, count, c);
    56. ......
    57.  
    58. delete []xData;
    59. delete []yData;
    60.  
    61. ......
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 

    Hope someone can help me.
    Thanks.
    Regards,
    sexgirl
    Last edited by wysota; 5th April 2012 at 12:25. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlotCurve plots quite slowly with 5K points

    Could anybody tell me where I make a mistake and how to fix the problem?
    Maybe my discription is hard to understand and forgive my broken english.

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotCurve plots quite slowly with 5K points

    Create small, compilable example of the issue and post it here.

    Posting semi-relevant parts of the code isn't useful.

    Also when creating the example there's high chance that you'll what was the cause of your problem.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlotCurve plots quite slowly with 5K points

    Well, for one thing, if you read the documentation for QwtPlotCurve::setRawData(), the pointers to the x and y arrays must be valid for the entire life of the curve. In lines 47-48, you create new arrays, then pass them into the curve, then in 58-59, you delete them. Thus, QwtPlotCurve is trying to draw using pointers that have already been deleted. If you resize your plot or do anything to cause a replot after the first time it draws those points, the program will probably crash. You have been lucky so far because QwtPlot makes a cache of the plot, which is re-built only when something changes to require a replot.

    Second, you are passing your data in as QList of QStringList of QString, then parsing 5000 points from deep down inside this structure from strings into doubles. Why? If all you are interested in is the y values buried down inside this complex data structure, find a way to pass them in as a QVector of doubles instead of converting them into strings and burying them.

    The slowness isn't Qwt's fault at all, it is the way you have chosen to represent and access your data. You are making your PC do so much extra work, I am surprised your PC didn't burn up.
    Last edited by d_stranz; 7th April 2012 at 17:31.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlotCurve plots quite slowly with 5K points

    Well, for one thing, if you read the documentation for QwtPlotCurve::setRawData(), the pointers to the x and y arrays must be valid for the entire life of the curve. In lines 47-48, you create new arrays, then pass them into the curve, then in 58-59, you delete them. Thus, QwtPlotCurve is trying to draw using pointers that have already been deleted. If you resize your plot or do anything to cause a replot after the first time it draws those points, the program will probably crash. You have been lucky so far because QwtPlot makes a cache of the plot, which is re-built only when something changes to require a replot.
    OK, when I look at your CustomData class again, it looks like maybe you are making a copy of the x-y arrays as a QwtArray inside od CustomData. You don't give the code for the CustomData::append() method, but if it is either copying or appending to the QwtArray, then this is OK.

    So if you are making a copy of the data, there is no need to create new arrays on the heap inside of updateEnergyplot(); use stack-allocated memory instead

    Qt Code:
    1. QwtArray< double > xData( count );
    2. QwtArray< double > yData( count );
    3.  
    4. for (int i = 0; i < count; i++)
    5. {
    6. xData[i] = (double)i;
    7. yData[i] = data.at(0).at(i).toDouble(&ok);
    8. }
    9. energyplot->appendDataShow( &(xData[0]), &(yData[0]), count, c);
    To copy to clipboard, switch view to plain text mode 

    Faster, and you don't fragment memory so much by repeatedly allocating and deleting the arrays.

  6. The following user says thank you to d_stranz for this useful post:

    sexgirl (15th May 2012)

Similar Threads

  1. Replies: 0
    Last Post: 3rd January 2012, 11:16
  2. QwtPlotCurve: maximum number of points ?
    By OverTheOCean in forum Qwt
    Replies: 2
    Last Post: 20th September 2010, 06:42
  3. QwtDial works slowly in QWS
    By simophin in forum Qwt
    Replies: 1
    Last Post: 19th July 2010, 07:00
  4. Application starting slowly.
    By aj2903 in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2008, 11:19
  5. very slowly apps
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2008, 11:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.