Re: Is there a STL equivalent of sprintf
- From: Andre Kaufmann <andre.kaufmann_re_move_@xxxxxxxxxxx>
- Date: Mon, 02 Mar 2009 19:40:36 +0100
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
.
- Follow-Ups:
- Re: Is there a STL equivalent of sprintf
- From: Stephen Howe
- Re: Is there a STL equivalent of sprintf
- References:
- Re: Is there a STL equivalent of sprintf
- From: Stephen Howe
- Re: Is there a STL equivalent of sprintf
- Prev by Date: Re: Is there a STL equivalent of sprintf
- Next by Date: Using list.splice unable to dereference iterator
- Previous by thread: Re: Is there a STL equivalent of sprintf
- Next by thread: Re: Is there a STL equivalent of sprintf
- Index(es):
Relevant Pages
|