Re: Is there a STL equivalent of sprintf
Stephen Howe <sjhoweATdialDOTpipexDOTcom> wrote:
Yep - but I think the whole iostream library should be
rewritten/redesigned with both in mind - simplicity >and< performance.
The STL for example is a good example - regarding performance.
I agree. While sprintf() is type unsafe and you can get buffer
overflow problems, it amounts to 1 function call. It is fast.
All those << operators are separate functions.
Not so much, as those are inlined. What definitely is a separate function
call is the invocation of formatters or parsers from the locale, as those
are implemented as virtual functions within a plugin mechanism.
I have yet to find an iostream package that is within 10% of stdio.
I think iostream needs a complete rethink.
Ahem, IOStreams are a flexible formatting and parsing framework that uses a
plugin mechanism for the actual work. This is _by far_ not functionally
equivalent to C stdio, so you are comparing apples to oranges. Further,
there are several pitfalls for performance:
1. Disable synchronisation with C stdio (ios_base::sync_with_stdio(false);).
2. Don't use obsolete implementations, e.g. VC6 used to increase the size of
a stringbuffer in steps of 32 bytes, IIRC, which is a performance nightmare
when writing more than that.
3. Use the C locale. Since this uses a fixed formatting, some steps could be
optimised or skipped there.
4. If you know the formatting you want, you can also use e.g. num_put
directly instead of indirectly through a stringstream.
That said, there definitely are some design issues within the IOStreams,
e.g. the naming of functions.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932