Results 1 to 19 of 19

Thread: Understanding why these few lines are slow

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Understanding why these few lines are slow

    This code is very slow in execution. By being event->mimeData()->urls().length() 15(so I have 15 urls), it takes almost 3-4 secs for it to execute. I am trying to figure out why, and how would I reduce this time. This is the code:
    Qt Code:
    1. for(int i=1; i<=event->mimeData()->urls().length(); i++){
    2. item=new QListWidgetItem;
    3. QUrl url = event->mimeData()->urls().takeAt(i-1);
    4. QString file2 = url.toString();
    5. QString file1 = file2.mid(7,file2.length()-2); //removing some things I don't want
    6. QString extension = file1.right(3); //taking last 3 letters from the string..
    7. if(extension=="png" || extension=="gif"){ //add to the listwidget
    8. item->setText(file1);
    9. ui->listWidget->addItem(item);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    I don't think I'm doing something that takes a lot time to be done... I convert QUrl to QString, do some processing to QString and go into an if statement. I am sure that adding items into the listwidget doesn't take too much time, because I have tested and I can add many thousands of files per second.
    So....what makes this code slow?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    Well, to be honest, that code looks terrible to me. Anyway, you are producing a memory leak. I would fix that first!

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

    hakermania (30th January 2011)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Understanding why these few lines are slow

    Since item is only a string, i don't think you need QListWidgetItem and allocated on heap. So first as Lykurg said remove the leak, and i would add remove the heap allocations (they can be expensive in loops), so first try it like this:
    Qt Code:
    1. for(int i=1; i<=event->mimeData()->urls().length(); i++){
    2. //item=new QListWidgetItem;
    3. QUrl url = event->mimeData()->urls().takeAt(i-1);
    4. QString file2 = url.toString();
    5. QString file1 = file2.mid(7,file2.length()-2); //removing some things I don't want
    6. QString extension = file1.right(3); //taking last 3 letters from the string..
    7. if(extension=="png" || extension=="gif"){ //add to the listwidget
    8. //item->setText(file1);
    9. ui->listWidget->addItem(file1); //use the addItem overload see the link
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Link

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

    hakermania (30th January 2011)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    Quote Originally Posted by Zlatomir View Post
    and i would add remove the heap allocations
    Have you? You don't see it in your code but:
    Qt Code:
    1. void QListWidget::insertItem(int row, const QString &label)
    2. {
    3. d->listModel()->insert(row, new QListWidgetItem(label));
    4. }
    To copy to clipboard, switch view to plain text mode 
    And as you might guess, addItem is just a convenient function for insertItem, so you could at least save one function call by using insertItem direct. But the real problems, to point them out, are in the takeAt() like, the "incorrect" use of mid(), the loop itselfs...

  7. The following user says thank you to Lykurg for this useful post:

    Zlatomir (30th January 2011)

  8. #5
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Understanding why these few lines are slow

    Thx both for the replies!
    I removed it, and, actually, this isn't the problem. I tried with 57 urls and it takes around 7 seconds to add them(!)
    And, the code posted is the only code inside the function... I begin to believe that string comparison takes too long?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  9. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    Well I don't will write you the code but, wouldn't it be smarter to check the extension first, before removing something from the front? (Just came now in my mind...)

  10. The following user says thank you to Lykurg for this useful post:

    hakermania (30th January 2011)

  11. #7
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Understanding why these few lines are slow

    Thanks, you're right, just did it:
    Qt Code:
    1. for(int i=1; i<=event->mimeData()->urls().length(); i++){
    2. QUrl url = event->mimeData()->urls().takeAt(i-1);
    3. QString file2 = url.toString();
    4. QString extension = file2.right(3);
    5. if(extension=="png" || extension=="gif"){
    6. QString file1 = file2.mid(7,file2.length()-2);
    7. ui->listWidget->addItem(file1);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    I removed the string comparison for testing and it's the same without it, so it hasn't to do with it....hmmm...
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  12. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    Ok, next, have a look at the sources of QMimeData::urls() and decide if it is a wise decision to call it all over again and again...

  13. The following user says thank you to Lykurg for this useful post:

    hakermania (5th February 2011)

  14. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Understanding why these few lines are slow

    It sure is not a good idea (a serious flaw indeed) to alter a list inside a loop where one of the loop parameters is based on that list.
    In other words, do not call a list size when you change that list inside the loop! Nasty things happen.
    (Might not be related to your problem though)

  15. The following user says thank you to tbscope for this useful post:

    hakermania (5th February 2011)

  16. #10
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Understanding why these few lines are slow

    Try this :

    Qt Code:
    1. QList<QUrl> urlList = event->mimeData()->urls();
    2.  
    3. for (QList<QUrl>::const_iterator i = urlList.begin();
    4. i != urlList.end();
    5. i++)
    6. {
    7. QString strFile2 = (*i).toString();
    8. if (strFile2.endsWith(".png") || strFile2.endsWith(".gif")) {
    9. QString strFile1 = strFile2.mid(7, strFile2.length() - 2);
    10. ui->listWidget->addItem(strFile1);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  17. The following user says thank you to Sven for this useful post:

    hakermania (5th February 2011)

  18. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    Quote Originally Posted by Sven View Post
    Try this
    We should encourage people to think about problems and to came up with a solution of their own. There is no benefit of simply paste a better code/solution. Furthermore, can you explain me the meaning of subtraction of 2 in this line:
    Qt Code:
    1. QString strFile1 = strFile2.mid(7, strFile2.length() - 2);
    To copy to clipboard, switch view to plain text mode 
    ?

  19. The following user says thank you to Lykurg for this useful post:

    hakermania (5th February 2011)

  20. #12
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Understanding why these few lines are slow

    Quote Originally Posted by Lykurg View Post
    We should encourage people to think about problems and to came up with a solution of their own. There is no benefit of simply paste a better code/solution. Furthermore, can you explain me the meaning of subtraction of 2 in this line:
    Qt Code:
    1. QString strFile1 = strFile2.mid(7, strFile2.length() - 2);
    To copy to clipboard, switch view to plain text mode 
    ?
    Ok I have looked into the code again and i see that this makes no sense. For example we have got:



    So , i guess he wants to remove the http:// and the extension ?
    If so, that
    Qt Code:
    1. QString strFile1 = strFile2.mid(7, strFile2.length() - 2);
    To copy to clipboard, switch view to plain text mode 

    makes no sense. Maybe this solution would be better: (we have checked already for .png and .gif, so this should be correct)

    Qt Code:
    1. QString strFile1 = strFile2.mid(7, strFile2.lastIndexOf("."));
    To copy to clipboard, switch view to plain text mode 

  21. The following user says thank you to Sven for this useful post:

    hakermania (5th February 2011)

  22. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Understanding why these few lines are slow

    To say it: First parameter indicates the starting point, last the length of the "resulting" string, so strFile2.length() - 2 is too long since only strFile2.length() - 7 left. Thus one should simply pass -1 as a last parameter. That is what I was trying to tell. Furthermore QString::remove()is probably better for his intention - I guess.

  23. #14
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Understanding why these few lines are slow

    Thank all of you, Sven's solution worked, but Lykurg's right, I don't understand why the code provided is better than my first posted, so i really want to understand my mistake in order to avoid it to the feature?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

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

    Default Re: Understanding why these few lines are slow

    Line #3 removes an item from a copy of the list that is immediately discarded. I don't know if that's relevant, what it is supposed to do and why you start iterating from 1. It's surely not very optimal. And I would like to know how come you deduced this loop takes three seconds to execute.
    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.


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

    hakermania (5th February 2011)

Similar Threads

  1. Understanding RGB888
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 29th August 2010, 20:03
  2. Having trouble understanding brush
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 30th July 2010, 18:18
  3. Better understanding of the ModelView archtecture
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 28th June 2010, 07:07
  4. Help understanding QWT Contour Plot
    By jwieland in forum Qwt
    Replies: 11
    Last Post: 7th December 2009, 06:47
  5. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 20:02

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
  •  
Qt is a trademark of The Qt Company.