jsguy
jsguy

Reputation: 2179

should you use fread/fwrite or read/write system calls when you want to specify the buffer size explicitly?

I have an application where I want to read/write data from/to a hard drive, but at the same time I want to specify the size of the buffer that is going to be used in order to make the application more efficient (I/O related).

I implemented two versions, one using read/write system calls and one using fread/fwrite functions where to specify the buffer size I made a call to the setvbuf function.

I noticed however a difference in the running times for large inputs in the order of 2.

This raised some questions as to whether using read/write system calls should always be preferred when you want to exploit the ability of transfering blocks of data from the disk in order to minimize the total amount of I/Os.

Upvotes: 1

Views: 364

Answers (1)

kennyzx
kennyzx

Reputation: 12993

It depends on how much data to read/write, if you set the buffer to be very tiny, it is inefficient to read/write large amount of data.

Here is a comparison.

Upvotes: 1

Related Questions