2

I have a Qt C++ program that process some data in a loop.

It appends each data processing result to the end of a text file. Data processing operations are placed in a loop, so the program can produce more that 800 results in a few seconds and write them one by one (i.e. separately).

I thought that so much I/O operations are not very good for computer's hard drive, so I organized an array where I store data processing results, and when array length is more than 200, the program appends it to file and clean, than the array becomes more that 200 again, and so on.

But I don't know exactly - is it really needed? Maybe it is just a waste of RAM and I should append data to text file without those arrays (buffers)? After all, the program writes the same amount of data, independently in which way it does.

4
  • 3
    It's overkill. You can expect your operating system to automatically perform what you are trying to do. Commented Apr 21, 2013 at 3:01
  • 1
    Don't worry, your hard drive will have a Disk buffer , which is a memory cache system , it is ensured that this cache is filled first before a disk write. For Example a 8kb cache , will wait till 8kb of data fills up, and then writes it to the drive as a single 8kb block of data Commented Apr 21, 2013 at 3:06
  • 1
    If you were hitting up unbuffered sys-calls, I'd say it isn't unwise, but I highly doubt you're that deep in the IO layer, so what you're doing is likely complete overkill. Commented Apr 21, 2013 at 3:07
  • Thank you all for a detailed replies! I won't use buffer in my program anymore. Commented Apr 21, 2013 at 3:09

1 Answer 1

1

Generally speaking, since c++ mechanism is providing with buffered io, you can change it as you like. In you context, "800 results" you referred does not tell the exact size of your output data. So You may be overskilled. F.Y.I, the function ios::rdbuf() will get a pointer to a streambuf object that represents the internal buffer for the stream. Then You can call streambuf::pubsetbuf(char * s, streamsize n) to set a new internal buffer with a given size. You can also find detail from http://en.cppreference.com/w/cpp/io/basic_ios/rdbuf.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.