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. Optimal buffer size
Servers for Qt installer are currently down

Optimal buffer size

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 4.3k Views 2 Watching
  • 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 8 Jun 2016, 05:50 last edited by
    #1

    Hi All
    I am using following

    class myScintilla:public QScintilla {
    public:
    readFile();

    private:

    };

    myScintilla:: readFile() {

    if (FILE* fp = f:open(ofilename.toLatin1(), "r")) {
    QTextStream ts(fp, QIODevice::ReadOnly);
    int bufferSize =1024*1024;
    do {
    QString s = ts.read(bufferSize);
    append(s);
    } while(!ts.atEnd());

    }

    Since we have some upper limit for memory of QString . I want to know what is limit of memory for QStrign on a 32 bit machine and

    I want to know what is the optimal bufferSize in the above code so that we do not get any runtime issue .

    My Experiments says

    for 800 MB file : for bufferSize size of 1MB (1024*1024;) it took 13 seconds
    For 800 MB file . for bufferSize size of 8KB it took 27 seconds

    Can anyone suggest optimal bufferSize in the above code so that we do not get any runtime issu

    K 1 Reply Last reply 8 Jun 2016, 12:35
    0
    • Q Qt Enthusiast
      8 Jun 2016, 05:50

      Hi All
      I am using following

      class myScintilla:public QScintilla {
      public:
      readFile();

      private:

      };

      myScintilla:: readFile() {

      if (FILE* fp = f:open(ofilename.toLatin1(), "r")) {
      QTextStream ts(fp, QIODevice::ReadOnly);
      int bufferSize =1024*1024;
      do {
      QString s = ts.read(bufferSize);
      append(s);
      } while(!ts.atEnd());

      }

      Since we have some upper limit for memory of QString . I want to know what is limit of memory for QStrign on a 32 bit machine and

      I want to know what is the optimal bufferSize in the above code so that we do not get any runtime issue .

      My Experiments says

      for 800 MB file : for bufferSize size of 1MB (1024*1024;) it took 13 seconds
      For 800 MB file . for bufferSize size of 8KB it took 27 seconds

      Can anyone suggest optimal bufferSize in the above code so that we do not get any runtime issu

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 8 Jun 2016, 12:35 last edited by
      #2

      @Qt-Enthusiast
      Hello,

      Since we have some upper limit for memory of QString . I want to know what is limit of memory for QStrign on a 32 bit machine

      Theoretically the whole addressable memory (4GB). In practice its somewhat smaller though.

      I want to know what is the optimal bufferSize in the above code so that we do not get any runtime issue .

      Generally bigger buffers tend to perform faster, but there's saturation to that effect. It depends on the application itself, the OS, the drivers and ultimately the hardware, so there's no "correct" size. For files of the size up to 1-2 GB I'd think 1MB is reasonable, for larger files I'd consider making the buffer larger as well.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on 8 Jun 2016, 14:24 last edited by
        #3

        Also it will be helpful what is maximum limit for QTextStream

        regards
        Roshni

        K 1 Reply Last reply 8 Jun 2016, 14:28
        0
        • Q Qt Enthusiast
          8 Jun 2016, 14:24

          Also it will be helpful what is maximum limit for QTextStream

          regards
          Roshni

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 8 Jun 2016, 14:28 last edited by
          #4

          @Qt-Enthusiast

          Also it will be helpful what is maximum limit for QTextStream

          There's no such thing. There's an internal buffer in QTextStream but it bears no consequence here. It'll be flushed on device close or on QTextStream::endl/QTextStream::flush. So the stream has no notion of size, it's a stream.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on 9 Jun 2016, 01:41 last edited by
            #5

            Just to clear my understanding what if , if I have a file of size 20 GB and I do

            FILE *fp = fopen("myfile.txt","r");
            QTextStream ts(fp, QIODevice::ReadOnly);

            is there internal buffer has memory limit to hold the data

            K 1 Reply Last reply 9 Jun 2016, 10:46
            0
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on 9 Jun 2016, 07:24 last edited by
              #6

              Hi
              I tried this to calculate the buffer size/ Can any one comment on it

              // myEditor.h
              class myEditor : QScintilla {
              public:
              readFile();
              };

              #include "myEditor.h"
              // myEditor.cc
              myEditor::readFile() {
              FILE* fp = fopen("mynew.v","r"):
              QTextStream ts(fp, QIODevice::ReadOnly);
              /* reading the text stream buffer by buffer
              bufferSize is calculated using following formula
              2 to power(k) * n = 2 to power 31*
              where n is size of each block in linux filesystem
              n is 4096 in my case and
              since ts.read returns QString and return value of QString::size is int
              thus 31 is for that limit */

              int bufferSize =(1024* 1024)/2;
              do {
              QString s = ts.read(bufferSize);
              append(s);
              } while(!ts.atEnd());
              }

              Please comment

              1 Reply Last reply
              0
              • Q Qt Enthusiast
                9 Jun 2016, 01:41

                Just to clear my understanding what if , if I have a file of size 20 GB and I do

                FILE *fp = fopen("myfile.txt","r");
                QTextStream ts(fp, QIODevice::ReadOnly);

                is there internal buffer has memory limit to hold the data

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 9 Jun 2016, 10:46 last edited by
                #7

                @Qt-Enthusiast

                is there internal buffer has memory limit to hold the data

                There's an internal buffer, but it's for temporary storage and will grow when needed. The data itself is on the disk until it's read.

                tried this to calculate the buffer size

                I have no clue what this code is supposed to do, or why are you insistent on knowing how large the text stream buffer is.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on 10 Jun 2016, 04:18 last edited by
                  #8

                  @kshegunov said:

                  I have no clue what this code is supposed to do, or why are you insistent on knowing how large the text stream buffer is.

                  Because in our case File sizes can go to 2 -3 GBS . so I am insistent so that I want to know that the tool does not crash even for the following line

                  QTextStream ts(fp, QIODevice::ReadOnly);

                  K 1 Reply Last reply 13 Jun 2016, 22:30
                  0
                  • Q Qt Enthusiast
                    10 Jun 2016, 04:18

                    @kshegunov said:

                    I have no clue what this code is supposed to do, or why are you insistent on knowing how large the text stream buffer is.

                    Because in our case File sizes can go to 2 -3 GBS . so I am insistent so that I want to know that the tool does not crash even for the following line

                    QTextStream ts(fp, QIODevice::ReadOnly);

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 13 Jun 2016, 22:30 last edited by
                    #9

                    @Qt-Enthusiast
                    Sorry for the late reply, I somehow missed the notification.

                    Because in our case File sizes can go to 2 -3 GBS

                    The problem is not with the QTextStream in this case. It's with how much of the file you want to read. On a 32 bit OS this is close to impossible, because you'd be pushing the memory limit reading a file that big in one go. An alternative to reading the whole file is to process it in parts instead. Perhaps the QFile's memory mapping API might be of use in your case.

                    I want to know that the tool does not crash even for the following line

                    QTextStream ts(fp, QIODevice::ReadOnly);
                    

                    This does next to nothing. It will create an object that wraps around the QIODevice/FILE * and will have an empty buffer. As I said it's a stream, it doesn't read anything by itself, and it has no notion of size. Anything you read from the file will be returned, anything you write to the stream will be buffered and actually written when the stream is flushed. That's all. There's no big secret with the buffer or its size. It's just put there for a minor optimization (and has no bearing on your problem here, as I stated before).

                    Kind regards.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    2

                    1/9

                    8 Jun 2016, 05:50

                    • Login

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