Why buffered I / O is faster than unbuffered I / O

While reading this, I found a reasonable answer that says:

Case 1: Directly Writing to a File on Disk

100 times x 1 ms = 100 ms

      

I understood that. Then

Case 3: Buffering in memory before writing to a file on disk

(100 times x 0.5 ms) + 1 ms = 51 ms

      

I didn't get it 1ms. What's the difference between writing 100 data to disk and writing 1 data to disk? Why do they both cost 1ms?

+3


source to share


4 answers


Disk access (transferring data to disk) is not performed byte-by-byte, it happens in blocks. So we cannot conclude that the time taken to write a 1

data byte is 1

ms, then the x

data bytes will take x

ms. This is not a linear relationship.

The amount of data written to disk at a time depends on the block size. For example, if the disk access cost is 1ms and the block size is 512 bytes, then writing from 1 to 512 bytes will cost you the same, only 1ms.

So back to the equator, if you have, say, 16 bytes of data to be written in each release for 20 iterations, then

  • for the case of direct recording


time = ( 20

iteration * 1

ms) == 20

ms.

  • for buffered access

time = ( 20

iteration * 0.5

ms (buffering time)) + 1

ms (to write all at once) = 10

+ 1

== 11

ms.

+7


source


This is due to the physical functioning of the disk. They can take larger buffers (called pages) and store them in one go. If you want to save data all the time, you need multiple changes on the same page, if you do it with a buffer, you edit the fast memory and then save everything in one go.



His example explains the operating costs. To load memory to data, you have 100 operations at a cost of 0.5s and then you have one of the changes in disk performance (I / O operation) that is not described in the answer and probably not obvious, almost the entire disk provides a bulk transfer change operation. So 1 I / O operation 1 saves to disk, not necessarily storing 1 bit (it could be a lot more data).

+1


source


Among other things, data is written to disk only in whole "blocks". The block is usually 512 bytes. Even if you only change one byte within a block, the OS and disk will have to write all 512 bytes. If you change all 512 bytes in the block before writing, the actual write will be no slower than changing just one byte.

Automatic caching within the OS and / or disk does indeed avoid this problem to a great extent. However, every "real" write operation requires a call from your program to the OS and probably all the way to the disk driver. It takes some time. By comparison, writing to a char / byte / ... array in your own in-memory process costs next to nothing.

0


source


When writing 1 byte for each record, you need:

  • disc seek time (which may vary) to place the "head" on top of the correct track on the disc,
  • disk rotation delay time while waiting for the correct disk sector under the "head".
  • disk read time during sector read (rotation delay time and sector read time may be required more than once if the CRC does not match the one stored on the disk.
  • insert the new byte at the desired location in the sector
  • rotation latency waiting for the corresponding sector to be under the heading again
  • sector write time (including new CRC).

Repeating all of the above for each byte (especially since disk is orders of magnitude slower than memory) takes a long time.

It is no longer necessary to write an entire sector of data than to update one byte.
This is why writing to a buffer full of data is much faster than writing several individual bytes.

There are other overheads such as updating inodes that:

  • track directories
  • track a single file

Each of these index files and files is updated every time the file is updated.

These inodes are (just) other sectors on the disk. In general, a lot of disk activity happens every time a file is changed.

Thus, changing a file only once, rather than many times, is a major time saver. Buffering is a technique used to minimize the number of disk operations.

0


source







All Articles