Results 1 to 20 of 21

Thread: QFileInfo is very slow

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 15:22.

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

    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,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    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,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    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!

Similar Threads

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