Results 1 to 6 of 6

Thread: Text file parsing too slow

  1. #1
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Text file parsing too slow

    Hello,

    I need to parse a tab delimited (or csv) file and display it in a QTableWidget.
    So far, I do it this way :

    Qt Code:
    1. while (!MyStream.atEnd())
    2. {
    3. General_Interface.MainTable->insertRow(Max_Number_of_Lines+1);
    4. buffer_line = MyStream.readLine();
    5. buffer_list = buffer_line.split(Delimiter);
    6.  
    7. for (int column = 0; column <= Max_Number_of_Columns; column ++)
    8. {
    9. QTableWidgetItem *Cell_Content = new QTableWidgetItem();
    10. Cell_Content->setText(buffer_list[column]);
    11. General_Interface.MainTable->setItem(Max_Number_of_Lines, column,
    12. Cell_Content);
    13.  
    14. }
    15.  
    16. Max_Number_of_Lines++;
    17. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that the kind of file I have to parse can contains more than 30 columns and 5000 lines, and in these cases it's awfully slow.

    How can I optimize this ?
    Is it relevant to use a QTextStream as I did ?

    Thanks in advance...

  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: Text file parsing too slow

    I wouldn't use a text stream in this situation - it only slows everything down without giving any benefits. You can operate on QFile directly.

    The problem might be using QTableWidget instead of QTableView. I'd suggest switching to model based approach - this should make things much faster, especially if you first fill the whole model and then set it on the view and not the other way round.

  3. #3
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text file parsing too slow

    Thanks for your fast response Wysota.

    By
    You can operate on QFile directly.
    do you mean by using QByteArray ?

    Concerning the model/view approach, I will rethink my program in this way. For it is the first time I use Qt, item-based approach seemed more friendly to me...
    But it seems that the easy way is not always the best !

  4. #4
    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: Text file parsing too slow

    Quote Originally Posted by Potch View Post
    do you mean by using QByteArray ?
    No, I mean using QIODevice::readLine(). QFile is a QIODevice.

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

    Potch (11th June 2008)

  6. #5
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text file parsing too slow

    Thanks for your response. I will go in this direction....

  7. #6
    Join Date
    Apr 2008
    Location
    Fons Outre-Gardons
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile [Resolved] Text file parsing too slow

    Hi all,

    As advised, I finally used QIODevice::readLine() with a model / view approach, and it works very fast. I easily load a 3 or 4 Mo text file quasi instantaneaous.

    For those that it can help :
    Qt Code:
    1. InputDataModel.setRowCount(0) ;
    2. InputDataModel.setColumnCount(Max_Number_of_Columns + 3) ;
    3.  
    4. while (InputDataFile.canReadLine())
    5. {
    6. if (Max_Number_of_Lines == 0)
    7. {
    8. buffer_list = buf.split(Delimiter) ;
    9. }
    10.  
    11. else
    12. {
    13. QByteArray line = InputDataFile.readLine();
    14. QString buffer_line(line);
    15. buffer_list = buffer_line.split(Delimiter);
    16. }
    17.  
    18. for (int column = 0; column <= Max_Number_of_Columns; column ++)
    19. {
    20. QStandardItem *item = new QStandardItem(buffer_list[column]);
    21. InputDataModel.setItem(Max_Number_of_Lines, column, item) ;
    22. }
    23. }
    24.  
    25. MytableView->setModel(&InputDataModel);
    26. MytableView->show() ;
    To copy to clipboard, switch view to plain text mode 

    Thanks again for your clever advices.....
    Last edited by Potch; 11th July 2008 at 21:21. Reason: updated contents

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Huge Text File
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2008, 19:23
  3. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  4. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.