Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Advise On Reading Lots Of Records From A File
QtWS25 Last Chance

[solved] Advise On Reading Lots Of Records From A File

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DBoosalis
    wrote on 23 Apr 2014, 14:08 last edited by
    #1

    I hope someone here can offer a trick or a few words of wisdon on how best to optimize reading lines from a file. I have been using QIODevice::readLine(), but for a file with line numbers approaching 230K it seems kind of slow. Does readLine() do any caching? Should I try to do my own caching with a QIODevice:;readAll(). With a readAll how do I then parse the big QByteArray to get sub QByteArrays terminated on eol. Perhaps I should I drop down and use Posix calls for reading large files? One more thing concerning Qt:

    In looking at the 5.x documentation there is a virtual method:
    QIODevice::readLineData(char * data, qint64 maxSize)

    This is in fact called by QIODevice::readLine, and the documentation has the following:

    "Buffered devices can improve the performance of readLine() by reimplementing this function"

    I guess my question is how can I reimplement this function to optimize for reading one line at a time. Thanks in advance for any help or suggestions. (Example code is always welcomed !)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 23 Apr 2014, 14:54 last edited by
      #2

      "seems kind of slow" is quite a broad term ;-) Are you using the code from the snippet in "QFile docs":http://qt-project.org/doc/qt-5/QFile.html?

      You can do readAll() and then split the result using "\n" as the delimiter (if you have set the QFile::Text flag first), although I am not sure you will gain anything by doing so.

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DBoosalis
        wrote on 23 Apr 2014, 15:02 last edited by
        #3

        Slow being like 25 seconds to load. Yes I read it in a loop till EOF and process each line. The readAll should be faster as it reads the whole file in one or two big chunks. Then I can parse in memory which has got to be very fast. My question is that once I have the QByteArray from a readAll(), how do I parse it with the "\n" deliminator as you suggested. Do I create a QStringList ?

        Thanks for taking the time to reply

        -david

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 23 Apr 2014, 15:07 last edited by
          #4

          Ah, so I suspect your line processing is eating that time. But I might be wrong, of course.

          Here:
          @
          QByteArray allData;
          // ...
          QStringList lines(QString(allData).split("\n"));

          foreach (const QString &line, lines) {
          // process each line
          }
          @

          Should work. It might be that QByteArray also has split() method, I am not sure.

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DBoosalis
            wrote on 23 Apr 2014, 16:05 last edited by
            #5

            [SOLVED] Thanks for your suggestions. I tried both ways: readAll() with line parses on the big byte array read in, and reading one line at a time. There was almost no difference (readAll() was actually a few hundred milliseconds slower on average for 100k records).

            For my file, which loaded in 208,000 records, it took 119 seconds. Clearly the time is in the processing for which I will see if there is a way to speed up.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 23 Apr 2014, 16:10 last edited by
              #6

              Right, happy coding! :-)

              (Z(:^

              1 Reply Last reply
              0

              1/6

              23 Apr 2014, 14:08

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved