Why is not more FreeBSD software written in C++?

Benjamin Lutz benlutz at datacomm.ch
Sun Apr 23 20:32:41 UTC 2006


Hello Dan,

On Sunday 23 April 2006 17:25, Dan Strick wrote:
> My results show something quite different.  Even though the buffer
> size used by Benjamin's write() test (61 bytes) is much larger than
> the one I used (only 24 bytes), the write() test to /dev/null still
> takes about 4 times longer than the putchar() test.  As one might
> expect, the write() test spends most of its time in the kernel doing
> all those write() system calls.  The putchar() test (i.e. stdio)
> is probably using a 1K byte buffer.

Your results are intriguing. I ran the tests again on a FreeBSD-6 machine (A 
Sempron 3000+), and I see the same thing, test_putchar runs faster and with 
much lower cpu load than test_write.

I'm not sure what to make of this. I would have said that the reason that 
write() is faster is because there's much fewer kernel calls necessary. I'm 
beginning to think though that FreeBSD's write(2) implementation is not as 
good as it could be. My Linux Mac Mini using a notebook HD actually runs 
test_write 10% faster than the FreeBSD Sempron 3000+ using proper SATA disks.

Or maybe Linux just cheats by doing some smart delaying/caching.

Thinking about it some more, I think you're right with your assumption that 
test_putchar is faster because stdio does some internal buffering. I ran 
ktrace on test_putchar (the version with 500'000 lines), and it calls write() 
4562 times on said FreeBSD system, each time with 0x1000 as last argument, ie 
it uses 4KB buffers. To confirm that buffering to be the reason, I increased 
the size of each line from 61 bytes to 16001 bytes, at which point the 
write() version showed a clear performance advantage on FreeBSD as well.

I had not thought about this possibility when I made my original statement. 
Given that, it makes sense that putchar is quite fast.

It still puzzles me though why the Mini (it's only a 1.25GHz G4!) beats the 
Sempron though.

> I stand by my suggestion that using putchar() might not be slower.
> On my system it was much faster.

Ok, I accept that then. If we think of a case where buffering must not happen, 
and thus a putchar() must be followed by a fflush(), would you agree that 
just using write() is faster?

Cheers
Benjamin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-chat/attachments/20060423/7948d4f8/attachment.pgp


More information about the freebsd-chat mailing list