Re: Is there a STL equivalent of sprintf

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Stephen Howe wrote:
On Thu, 19 Feb 2009 11:12:20 +0100, Andre Kaufmann
<andre.kaufmann_re_move_@xxxxxxxxxxx> wrote:

The C++ replacement for sprintf - stringstream - is O.K. for a small number of conversions.

Have you considered ostrstream or istrstream with a fixed size buffer?

Thank you for the hint - good idea.
I've played around with various optimization before and I think I've tested fix buffers too. But I'm not sure about that.
But to play it safe I tried it again - results see below.
I've also disabled synchronization etc. but got no significant speed increase.

You have to remember to nul terminate final input/output (or use ends
manipulator).

With no dynamic memory underneath, they are faster.

Yes, they are. But unfortunately they are still (much) slower than using sprintf. I think this is not only due to memory allocations but also due to a deep call hierarchy and complex code, which can't be easily optimized by the compiler.

Since sprintf is used internally in stringstreams I don't see a performance advantage to prefer the stream library for string conversion of integers. A simple wrapper function

typename<T> std::string ToString(const T& value);

Specialized for native types is IMHO more convenient and faster (if sprintf is used) than stringstream.


Yes deprecated but they will still be around 20 years from now.
They are in the standard.

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.

Stephen Howe

Andre
.



Relevant Pages

  • Re: [Lit.] Buffer overruns
    ... >>determining whether a program has a buffer overflow. ... > you get around undecidability is almost always the same; ... > optimization is undecidable; nonetheless, a good optimizer will only apply ... So maybe there's a useful subset of C that can be accurately ...
    (sci.crypt)
  • Re: Direct Copying To Share Memory In NDIS ProtocolReceive
    ... WaitXXX() functions that involve user-to-kernel transition before they can ... access the buffer. ... and see how it all works - I wold start thinking about optimization ...
    (microsoft.public.development.device.drivers)
  • Re: Performance of lists vs. list comprehensions
    ... buffer after its last expansion and no saved tuple). ... As near as I can determine, the CPython optimization you are referring to ... The optimization patch is here: ... algorithm is, ...
    (comp.lang.python)
  • Re: [Lit.] Buffer overruns
    ... >overflows: Say you've got a function fand you want to see if it ... >determining whether a program has a buffer overflow. ... them -- but you don't have the undecidability problem any further. ... optimization is undecidable; nonetheless, a good optimizer will only apply ...
    (sci.crypt)
  • Re: Stream_Element_Array
    ... I would of course agree if the data type didn't occupy the full width of ... the buffer it was being converted into. ... Shouldn't be a similar problem with endianness conversions, ... Of course, if your hardware registers are 16 or 32 bits wide, that's a ...
    (comp.lang.ada)