For fun I run the tests on a SUN box I have here it's an old Sparc
200MHz Running Solaris 8.
and I used the gcc 3.4.2 compiler.
----------
time ./test_putchar >/dev/null
real 0m0.45s
user 0m0.44s
sys 0m0.01s
time ./test_write >/dev/null
real 0m0.66s
user 0m0.38s
sys 0m0.28s
time ./test_iostream >/dev/null
real 0m3.28s
user 0m3.12s
sys 0m0.04s
time ./test_string >/dev/null
real 0m1.50s
user 0m1.48s
sys 0m0.02s
----------
Don 8)
Benjamin Lutz wrote:
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
_______________________________________________
[email protected] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-chat
To unsubscribe, send any mail to "[EMAIL PROTECTED]"