Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: QFileInfo is very slow

  1. #1
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default QFileInfo is very slow

    Hi,
    i have a problem where QFileInfo is very slow.

    I have a QStringList with over 15000 paths. I want to get the lastModified date, the fileSize and the fileName. It is very slow. It took about 60-80 seconds.

    Qt Code:
    1. // collect files and exclude empty mask files
    2. for(int i=0;i<files3D.count();i++)
    3. {
    4. QFileInfo fileInfo = files3D[i];
    5.  
    6. QDateTime date = fileInfo.lastModified();
    7. QString str;
    8. str = date.toString("dd/MM/yyyy hh:mm:ss");
    9.  
    10. if(fileInfo.size() != 657660)
    11. {
    12. fileNames.append(fileInfo.fileName());
    13. fileDates << str;
    14. fileSizes << QString::number(fileInfo.size());
    15. }
    16.  
    17. qApp->processEvents();
    18. }
    To copy to clipboard, switch view to plain text mode 

    I think may code is quite simply or have i done something wrong here?
    Is there a faster way to get the infos from a file?
    Last edited by jho; 20th January 2015 at 14:22.

  2. #2
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileInfo is very slow

    For that many paths 60 to 80 seconds doesn't sound entirely unreasonable to me for file actions. Do you have to get the info for all of the files at the same time?

  3. #3
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    Yes I need all of the info at once.

    If I compare it with the simple dir command from windows it takes really long.

    qt: 80 sec
    dir command: 3 sec

    My fallback is to use the dir command. But this would be only a workaround.

  4. #4
    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: QFileInfo is very slow

    Your dir command does not process events after reading each entry
    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.


  5. #5
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    Hi wysota: I think you mean this line.

    qApp->processEvents();

    I put this in to keep my application responsive and not freezing. But I already disabled it. So it has nothing to do with the slow reading.

    Or do you mean my dir command approach. This would be more of a hack and is not really a good solution.

  6. #6
    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: QFileInfo is very slow

    This code:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char **argv) {
    4. QDir dir(argv[1]);
    5. QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot|QDir::Files);
    6. qDebug() << "File count:" << list.size();
    7. for(int i=0;i<list.size();++i) {
    8. const QFileInfo finfo = list.at(i);
    9. QDateTime mtime = finfo.lastModified();
    10. }
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    takes this much time on a directory containing 15000 files:
    $ time ./dirtest dir
    File count: 15000

    real 0m0.095s
    user 0m0.074s
    sys 0m0.020s

    Bottom line: QFileInfo is not slow.
    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.


  7. #7
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    I have to look into it again. Maybe with your code although it looks similar to mine. I have to say that the files are stored on a network. But the windows dir command reads it also from the network.

  8. #8
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    I have tested it. It seems to me that using an entry from a QFileInfoList list is way faster than using a QString from a QStringlist with QFileInfo. Very strange...

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFileInfo is very slow

    QDir::entryInfoList will create all of the QFileInfo objects in one pass of the directory. If you are getting file names into a QStringList and then creating a QFileInfo for each file, then you are accessing the directory multiple times.

    Is that what you are referring to? If so, makes sense to me it's way slower, if not, post some code to show what you're referring to.

  10. #10
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    Yes, that is what i meant. And your explanation sounds valid.

    Thanks guys for your help.

  11. #11
    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: QFileInfo is very slow

    Quote Originally Posted by jthomps View Post
    QDir::entryInfoList will create all of the QFileInfo objects in one pass of the directory. If you are getting file names into a QStringList and then creating a QFileInfo for each file, then you are accessing the directory multiple times.

    Is that what you are referring to? If so, makes sense to me it's way slower,
    This code:

    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char **argv) {
    4. QDir dir(argv[1]);
    5. QStringList list = dir.entryList(QDir::NoDotAndDotDot|QDir::Files);
    6. qDebug() << "File count:" << list.size();
    7. for(int i=0;i<list.size();++i) {
    8. const QFileInfo finfo = QFileInfo(list.at(i));
    9. QDateTime mtime = finfo.lastModified();
    10. }
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    takes this much (different computer, don't compare with previous results):
    $ time ./dirtest dir
    File count: 15000

    real 0m0.068s
    user 0m0.055s
    sys 0m0.012s

    I don't see any significant differences.
    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.


  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFileInfo is very slow

    Quote Originally Posted by wysota View Post
    I don't see any significant differences.
    I stand corrected. Looks like the 2nd test was actually a little faster than the first, go figure!

  13. #13
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    I can post my solution tomorrow.

    Maybe I have such great performance impact because i am reading the files from a network?

  14. #14
    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: QFileInfo is very slow

    Quote Originally Posted by jthomps View Post
    I stand corrected. Looks like the 2nd test was actually a little faster than the first, go figure!
    No, as I said it was done on a different machine so you shouldn't compare them.
    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.


  15. #15
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFileInfo is very slow

    Sorry, missed the part about run on different computer.

  16. #16
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileInfo is very slow

    Your code copies a QFileInfo instance at each iteration of the loop. I have no idea how efficient the copy is (the doc does not mention implicitly sharing, and I have not had a look at its implementation), but you could try
    Qt Code:
    1. const QFileInfo &fileInfo = files3D.at(i);
    To copy to clipboard, switch view to plain text mode 
    instead.

    For very populated directories, QDirIterator is a better option that QDir.entry*List() because it lets you extract and store exactly the information you need.

    In any case, the measurements provided by the other posters suggest that other, more significant, sources of inefficiency may exist in your code. You should fix those first.

  17. #17
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    I already tried adding "const" and it definetly makes a difference here. Keep in mind that I run the code on a network drive.

    Here is my code so far. It takes 1 sec for 25000 files and this is enough for me.

    Qt Code:
    1. QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot|QDir::Files);
    2.  
    3. for(int i=0;i<list.size();++i)
    4. {
    5.  
    6.  
    7. const QFileInfo fileInfo = list[i];
    8.  
    9. const QDateTime date = fileInfo.lastModified();
    10. const QString str = date.toString("dd/MM/yyyy hh:mm:ss");
    11.  
    12. if(fileInfo.size() != 657660)
    13. {
    14. fileNames << fileInfo.fileName();
    15. fileDates << str;
    16. fileSizes << QString::number(fileInfo.size());
    17. }
    18.  
    19. qApp->processEvents();
    20. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileInfo is very slow

    Quote Originally Posted by jho View Post
    I already tried adding "const" and it definetly makes a difference here.
    That is not what I meant. Your code still copies a QFileInfo. The statement I suggested in my previous post defines fileInfo as a const C++ reference to the ith element in the list (the "&" in the declaration is what makes it a reference).

  19. #19
    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: QFileInfo is very slow

    There is one thing which bothers me -- all your code does is that it has a list with the data you need (line #1) and you copy data from that list to three more lists (lines #14-16). What exactly is the point of doing that? It's a pure waste of time.
    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.


  20. #20
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileInfo is very slow

    i store them in a struct and return this struct at the end. I have three listWidgets where I need to add these three arrays.

Similar Threads

  1. QFileInfo
    By ishkabible in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2010, 00:08
  2. QFileInfo on QListWidgetItem
    By Phalanx in forum Qt Programming
    Replies: 0
    Last Post: 22nd April 2010, 20:33
  3. QFileInfo Creation
    By QbelcorT in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2009, 09:01
  4. Is there such a way QFileInfo
    By baray98 in forum Qt Programming
    Replies: 9
    Last Post: 21st April 2008, 03:23
  5. about QFileInfo
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2006, 10:09

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.