Reputation: 5280
Compared to functions like printf()
, system call write()
is usually slower because it has no buffer. But why not provide a built-in buffer in write()
. OSes' speed is important, isn't it?
I am not familiar with operating system, it quite confuses me.
Upvotes: 0
Views: 72
Reputation: 719606
Because they are the low level calls to the operating system on which the higher level (buffered) methods like fread
, fwrite
, fflush
and so on are implemented.
OSes' speed is important, isn't it?
Yes it is. But it is assumed that application is going to make write()
calls efficiently. If it needs buffering, it should use fwrite
.
So if read
is not buffered and buffering is more efficient, why don't they hide it?
Because buffering is not always more efficient. In fact, for some use-cases a buffered stream does more copying than using write
directly. For bulk I/O operations this extra copy leads to slower I/O.
Upvotes: 1