write or printf, which is faster? write or printf, which is faster? unix unix

write or printf, which is faster?


How, with ... the fact that printf itself calls write, is this possible? Is there something that I'm missing?

Yes, there is something that you are missing. printf doesn't necessarily call write every time. Rather, printf buffers its output. That is, it often stores its result in a memory buffer, only calling write when the buffer is full, or on some other conditions.

write is a fairly expensive call, much more expensive than copying data into printf's buffer, so reducing the number of write calls provides a net performance win.

If your stdout is directed to a terminal device, then printf calls write every time it sees a \n -- in your case, every time it is called. If your stdout is directed to a file (or to /dev/null), then printf calls write only when its internal buffer is full.

Supposing that you are redirecting your output, and that printf's internal buffer is 4Kbytes, then the first loop invokes write 3000000 / (4096 / 12) == 8780 times. Your second loop, however, invokes write 3000000 times.

Beyond the effect of fewer calls to write, is the size of the calls to write. The quantum of storage in a hard drive is a sector -- often 512 bytes. To write a smaller amount of data than a sector may involve reading the original data in the sector, modifying it, and writing the result back out. Invoking write with a complete sector, however, may go faster since you don't have to read in the original data. printf's buffer size is chosen to be a multiple of the typical sector size. That way the system can most efficiently write the data to disk.

I'd expect your first loop to go much faster than the second.


You are not comparing apples to apples, because the loop with write runs strlen 3000000 times, while printf does not do any of that; it does not do any formatting either, so "fancy formatting magic" hardly applies.

size_t len = strlen( "Test string\n" );for( i = 0; i < 3000000; i++ ) {    write( STDOUT_FILENO, "Test string\n", len );}

Another important difference is that printf flushes every time when you pass \n, while write does not. You should remove \n from both strings to make your benchmarks more equitable.