Re: Binary File
On Wed, 23 Aug 2006 00:08:51 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:
for(int i = 0; i < huge_number; i++)
_write(...);
vs.
for(int i = 0; i < huge_number; i++)
fputc(...);
I improved the performance of one product by a factor of 5 (literally) by replacing the
first with the second, because the code had been reading one character (it was 8-bit only)
on each kernel call. When I changed it to use stdio, the number of kernel calls for I/O
went down from 100,000+ calls to about 2000 calls.
I can't remember exactly what it was, but there was an instance of that
same sort of thing affecting a famous piece of software, maybe the original
Mosaic browser. Note that even fputc can be horribly slow in VC++ when
using the multithreaded CRT, because each and every call locks and unlocks
a critical section; I once measured the overhead to be a factor of 10 for a
tight loop. MS finally addressed this in VC 2005 with _fputc_nolock and
similar functions. (See also _lock_file/_unlock_file.)
My own preference is CStdioFile for text and CFile for binary. The advantage of CFile is
that I can do things like read the entire file contents into memory when the file is small
(where "small" means "no more than a few tens of megabytes"--it is also amazing how many
beginning programmers think these are "large" files. Large files are files that require
more than 32 bits to express the byte offset in the file...)
Much is made about iostreams and the << and >> operators, but when I look at my code I see
150K source lines and maybe 20 lines read or write files, so I find it hard to get excited
about << and >>. But if you ask a bunch of new C++ programmers why C++ is better than C,
they won't say "encapsulation", "abstraction", "derivation", "virtual methods",
"modularity", "constructors and destructors", "exception handling", or anything else we
see as important; they'll say "you don't need to know how to use those %-format things in
printf" because some textbook says this is cool, or some professor said this is cool.
Makes you wonder about how we're education people.
The nice things about iostreams are type safety and extensibility, but
doing non-trivial formatting with iostreams can be considerably more
verbose than with stdio, and they're equally bad for I18N programming. This
would seem to bring together the best of what iostreams and stdio have to
offer, and it adds positional parameters:
Boost Format library
http://www.boost.org/libs/format/index.html
--
Doug Harrison
Visual C++ MVP