Results 1 to 1 of 1

Thread: [solved] Alternatives to the slow QTextStream pos() method ?

  1. #1
    Join Date
    Feb 2012
    Posts
    10
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default [solved] Alternatives to the slow QTextStream pos() method ?

    Hi All,

    I'm trying to build a Component based on QPlainText edit to handle large text files (like 50Mo).
    My approach is to load the first line of the files, and to run a thread that will read the whole file doing two things:
    -Detect total line number to update the scrollbar maxvalue
    -Build indexes by keeping in memory the file offset every x lines (currently 1000) to fast seek into the file.

    The problem is that the pos() function is very slow to return its value. For exemple, reading a text file of 60Mo almost take 1s, but if I have to count get the offest, it takes 1 minute...

    Here is my code:

    Qt Code:
    1. void FileSpecUpdater::run()
    2. {
    3.  
    4. /* QFile file(_FileName);
    5.   if (file.open (QIODevice::ReadOnly)) //read write later
    6.   {
    7.   QMessageBox::critical(0, "Error opening File in Thread", "Couldn't open file: " + _FileName);
    8.   return;
    9.   } */
    10. QTextStream stream ( _FileHandle );
    11.  
    12. int lc=0;
    13. QString text;
    14. while( !stream.atEnd() ) {
    15. text = stream.readLine();
    16. lc++;
    17. QTime currTime = QTime::currentTime();
    18. if(lc%INDEX_EVERY == 0)
    19. {
    20. lineOffset lo;
    21. lo.lineNumber=lc;
    22. lo.Offset=0;
    23. // lo.Offset=stream.pos();
    24. _FileSpecsToUpdate->lineMaps.append(lo);
    25. qDebug()<< currTime.msecsTo(QTime::currentTime()) << " : " << lc;
    26. currTime = QTime::currentTime();
    27. }
    28. }
    29. _FileSpecsToUpdate->setLineNumber(lc);
    30. // emit finished();
    31. }
    To copy to clipboard, switch view to plain text mode 

    I get in the output:
    0 : 1000
    0 : 2000
    0 : 3000
    0 : 4000
    0 : 5000
    0 : 6000
    0 : 7000
    0 : 8000
    0 : 9000
    0 : 10000
    0 : 11000
    0 : 12000
    0 : 13000
    0 : 14000
    now if I uncomment "lo.Offset=stream.pos()", I get:

    61 : 2000
    95 : 3000
    120 : 4000
    151 : 5000
    180 : 6000
    214 : 7000
    242 : 8000
    275 : 9000
    302 : 10000
    328 : 11000
    363 : 12000
    401 : 13000
    440 : 14000
    450 : 15000
    486 : 16000
    517 : 17000
    539 : 18000
    573 : 19000
    606 : 20000
    632 : 21000
    It's even exponentially longer each time...

    Since i'm reading the file from scratch, maybe I can try to compute manually the file offset, but the problem is that I don't know if my lines will be terminated by \n or \n\r, thus it makes the result impredicable AFAIK.

    Now, the doc states "Because QTextStream is buffered, this function may have to seek the device to reconstruct a valid device position. This operation can be expensive, so you may want to avoid calling this function in a tight loop.", so anyone have any idea on how could I proceed ?


    Added after 48 minutes:


    I do like this now, works way better, even if it was more painful ...

    Qt Code:
    1. void FileSpecUpdater::run()
    2. {
    3. QByteArray buffer;
    4. _FileHandle->seek(0);
    5. int lc=0;
    6. int offset=0;
    7. while (!_FileHandle->atEnd())
    8. {
    9. buffer=_FileHandle->read(FILE_READ_BUFFER_SIZE);
    10. int c = buffer.count("\n");
    11. if((lc%INDEX_EVERY) > (lc+c)%INDEX_EVERY)
    12. {
    13. int lineDelta = (lc+c)%INDEX_EVERY;
    14. lineOffset lo;
    15. lo.lineNumber=lc + c - lineDelta;
    16. int curPos=0;
    17. for(int i = 0; i<(c - lineDelta-1); i++)
    18. {
    19. curPos=buffer.indexOf("\n",curPos+1);
    20. }
    21. lo.Offset=offset + curPos +1;
    22. _FileSpecsToUpdate->lineMaps.append(lo);
    23. }
    24. lc+=c;
    25. offset+=FILE_READ_BUFFER_SIZE;
    26. }
    27.  
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nekkro-kvlt; 19th August 2012 at 17:19.

Similar Threads

  1. Replies: 15
    Last Post: 24th December 2011, 12:07
  2. 7cs alternatives
    By mcosta in forum Qt Programming
    Replies: 0
    Last Post: 20th June 2011, 13:41
  3. Replies: 1
    Last Post: 25th November 2010, 11:37
  4. Alternatives for Phonon ?
    By dano in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2009, 11:19
  5. seek and pos function alternatives
    By uchennaanyanwu in forum Newbie
    Replies: 2
    Last Post: 5th August 2008, 09:46

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.