Results 1 to 8 of 8

Thread: QList speed

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question QList speed

    Which of the two scenarios is faster?

    1. Build a QList with for loop and append()
    2. Use the QList
    3. Delete the QList with clear()
    4. Cycle back to step 1 several millions of times

    1. Build a QList with for loop and append()
    2. Use the QList
    3. Zero the QList with a for loop
    4. Cycle back to step 2 several millions of times

    The Qlist is <double> and its size is about 5000

    So, basiclly what I'm, asking is: which is faster a) append()+clear(), or b) using the for loop to reassing.
    I'm suspecting the latter is faster but I don't know enough about how QList works.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList speed

    If you know the number of items upfront (so that you can reserve space for all items in one go) then they are (more or less) equally fast.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    timmu (11th September 2012)

  4. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList speed

    If you look at QList implementation you'll find the following code for the clear:
    Qt Code:
    1. ...
    2. Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
    3. {
    4. *this = QList<T>();
    5. }
    6. ...
    To copy to clipboard, switch view to plain text mode 
    You might also look at implementation of assignment operator:
    Qt Code:
    1. Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l)
    2. {
    3. if (d != l.d) {
    4. QListData::Data *o = l.d;
    5. o->ref.ref();
    6. if (!d->ref.deref())
    7. free(d);
    8. d = o;
    9. if (!d->sharable)
    10. detach_helper();
    11. }
    12. return *this;
    13. }
    To copy to clipboard, switch view to plain text mode 
    So, as you can see, there are d-pointer's data copy and ref-counting operations.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    timmu (11th September 2012)

  6. #4
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList speed

    So, basically it is fair to say that building ( append() ) and clearing ( clear() ) a QList is a fast way of doing things? I shouldn't replace as many append()/clear() pairs with loops assigning new values to existing QList to gain speed?

  7. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList speed

    how slow is your app? how much time do you hope to gain? have you ran profilers and found this area to be a bottle neck?

    The two options are very easy to test. So why don't you test and compare?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList speed

    Remeber that QList in debug mode have many asserts and is about several times slower than in release mode.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList speed

    Quote Originally Posted by timmu View Post
    So, basically it is fair to say that building ( append() ) and clearing ( clear() ) a QList is a fast way of doing things? I shouldn't replace as many append()/clear() pairs with loops assigning new values to existing QList to gain speed?
    It depends whether you know how many items you are going to have. Most of append() cost is reallocating memory. If you allocate a lot, it will be slower than reassigning data to existing cells in the list.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList speed

    most appends wont be allocating memory though...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. how to confirm the value of one QList in other QList quickly?
    By weixj2003ld in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2012, 20:48
  2. How to speed up the Qt GUI apps launch speed?
    By wshn13 in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2012, 08:37
  3. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42

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.