Results 1 to 4 of 4

Thread: are they similar in performance ?

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default are they similar in performance ?

    I have usage like

    Qt Code:
    1. foreach(Item *, items()) {
    2. // operation a on item
    3. // operation b on item
    4. // operation c on item
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now these operations a, b, c span multiple lines and the code looks messy.
    So i thought i can split them into different functions and the function can't be written in the form function(one item) instead the functions have to be of form function(ItemList)

    My question is will the performance remain same even if i use it this way
    (the number of items will be in hundreds and in worst case thousands)
    Qt Code:
    1. functionA(ItemList list) {
    2. foreach(Item *, list) {
    3. operation
    4. }
    5. }
    6.  
    7. functionB(ItemList list) {
    8. foreach(Item *, list) {
    9. operation
    10. }
    11. }
    12.  
    13. functionC(ItemList list) {
    14. foreach(Item *, list) {
    15. operation
    16. }
    17. }
    18.  
    19. // --------------------and finally in the client code
    20. functionA(list);
    21. functionB(list);
    22. functionC(list);
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: are they similar in performance ?

    The short answer i no, since foreach() copies the container returned by items(). This doesn't have to be a severe penalty since QList supports implicit sharing. If your code looks messy, why don't you put operation a, b and c in inlined functions, which you hide frome the client by making them private members or like this:

    Qt Code:
    1. namespace{
    2. inline void operationA(Item* item){...}
    3. inline void operationB(Item* item){...}
    4. inline void operationC(Item* item){...}
    5. }
    6.  
    7. void publicFuntion()
    8. {
    9. foreach(Item *, items()) {
    10. operationA(item);
    11. operationB(item);
    12. operationC(item);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    That way your code will remain clean aswell as the public interface.
    Last edited by spud; 12th January 2008 at 11:59. Reason: wrote yes when I meant no;)

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: are they similar in performance ?

    The short answer is NO. As you do three loops instead of one it'll take three times as much as it used to... If yo are doing this frequently and on big lists the first alternative is recommended, unless performance does not matter in your application. The solution offered by spud is probably the best as it clearly separate different operations and still performs a single loop
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: are they similar in performance ?

    Thanks for your replies but it is better i post code so that i can highlight fact that i can't write a function for single item.
    I have the following kind of usage occurring few times.

    Qt Code:
    1. QRectF rect = m_insertibles.isEmpty() ? QRectF() : m_insertibles.first()->sceneBoundingRect();
    2.  
    3. foreach(QucsItem *item, m_insertibles) {
    4. item->setSelected(true);
    5. item->setVisible(cursorOnScene);
    6. rect |= item->sceneBoundingRect();
    7. }
    8.  
    9. QPointF center = rect.center();
    10. QPointF delta = active->mapToScene(pos) - center;
    11.  
    12. foreach(QucsItem *item, m_insertibles) {
    13. item->moveBy(delta.x(), delta.y());
    14. }
    To copy to clipboard, switch view to plain text mode 

    Well i thought i could write convienience functions like say

    Qt Code:
    1. QPointF centerOfItems(const QList<QGraphicsItem*> &_list);
    2. void moveItemsBy(QList<QGraphicsItem*> &_list, QPointF delta);
    3. void setVisibilityofItems(QList<QGraphicsItem*> &_list, bool visiblity);
    To copy to clipboard, switch view to plain text mode 

    But as you can see the three implementations will have repetitions of loop. Ofcourse trying to be optimistic, the code isn't that clumsy
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. Performance problems with overlapping qgraphicsitems
    By brjames in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2008, 21:42
  2. Tabbed widget performance suggestions
    By MrGarbage in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2007, 16:02
  3. Poor performance with Qt 4.3 and Microsoft SQL Server
    By Korgen in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 10:28
  4. Replies: 1
    Last Post: 4th October 2006, 16:05
  5. Increasing performance from Qtextedit, listview, etc?
    By taylor34 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 10:20

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.