* Lexical conversion.
@ 2002-10-14 20:29 John Carter
2002-10-15 1:50 ` Claudio Bley
0 siblings, 1 reply; 5+ messages in thread
From: John Carter @ 2002-10-14 20:29 UTC (permalink / raw)
To: gcc-help
What is the fastest way under gcc-3.* to convert from an int to a string?
ie. Equivalent to this yucky bit of code...
string convert( int number)
{
char line[80];
sprintf( line, "%ld", number);
return string(line);
}
--
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand
Good Ideas:
Ruby - http://www.ruby-lang-org - The best of perl,python,scheme without the pain.
Valgrind - http://developer.kde.org/~sewardj/ - memory debugger for x86-GNU/Linux
Free your books - http://www.bookcrossing.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Lexical conversion.
2002-10-14 20:29 Lexical conversion John Carter
@ 2002-10-15 1:50 ` Claudio Bley
2002-10-15 17:14 ` John Carter
0 siblings, 1 reply; 5+ messages in thread
From: Claudio Bley @ 2002-10-15 1:50 UTC (permalink / raw)
To: John Carter; +Cc: gcc-help
>>>>> "John" == John Carter <john.carter@tait.co.nz> writes:
John> What is the fastest way under gcc-3.* to convert from an int
John> to a string? ie. Equivalent to this yucky bit of code...
We are talking about C++, right? I don't know how fast it is (or what
your constrains are), but I would use something like that:
#include <sstream>
string convert (const int number) {
ostringstream ostr;
ostr << number;
return ostr.str ();
}
--
Claudio Bley ASCII ribbon campaign (")
Debian GNU/Linux advocate - against HTML email X
http://www.cs.uni-magdeburg.de/~bley/ & vCards / \
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Lexical conversion.
2002-10-15 1:50 ` Claudio Bley
@ 2002-10-15 17:14 ` John Carter
0 siblings, 0 replies; 5+ messages in thread
From: John Carter @ 2002-10-15 17:14 UTC (permalink / raw)
To: Claudio Bley; +Cc: gcc-help
On Tue, 15 Oct 2002, Claudio Bley wrote:
> >>>>> "John" == John Carter <john.carter@tait.co.nz> writes:
>
> John> What is the fastest way under gcc-3.* to convert from an int
> John> to a string? ie. Equivalent to this yucky bit of code...
>
> We are talking about C++, right? I don't know how fast it is (or what
> your constrains are), but I would use something like that:
>
> #include <sstream>
>
> string convert (const int number) {
> ostringstream ostr;
> ostr << number;
> return ostr.str ();
> }
Sigh! I decided (especially as most the cases I worry about are in 0-9) to
implement it like so.. (I unit tested it so I'm reasonable confident it
works and its about 3 times faster in the general case than using printf)
string convert(const int value) {
int abs_value;
if (value >= 0) {
// Optimise for single digit case...
if (value <= 9) {
return string( 1, '0'+value);
}
abs_value = value;
} else {
abs_value = -value;
}
char buf[100];
char * start = buf+100;
char * end = start;
do {
*(--start) = '0' + abs_value % 10;
abs_value /= 10;
} while (abs_value);
if( value < 0) {
*(--start) = '-';
}
return string(start, end-start);
}
--
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand
Good Ideas:
Ruby - http://www.ruby-lang-org - The best of perl,python,scheme without the pain.
Valgrind - http://developer.kde.org/~sewardj/ - memory debugger for x86-GNU/Linux
Free your books - http://www.bookcrossing.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Lexical conversion.
2002-10-16 6:03 Moore, Mathew L
@ 2002-10-16 12:20 ` Sebastian Huber
0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Huber @ 2002-10-16 12:20 UTC (permalink / raw)
To: gcc-help
On Wednesday 16 October 2002 06:03, Moore, Mathew L wrote:
> > > #include <sstream>
> > >
> > > string convert (const int number) {
> > > ostringstream ostr;
> > > ostr << number;
> > > return ostr.str ();
> > > }
> >
> > Sigh! I decided (especially as most the cases I worry about
> > are in 0-9) to
> > implement it like so.. (I unit tested it so I'm reasonable
> > confident it
> > works and its about 3 times faster in the general case than
> > using printf)
>
> Why do you suppose there is such a great speed improvement with the code
> below? It seems to me that snprintf() should be able to optimize this task
> quite effectively. The only overhead should be the parsing of the format
> string, which in this case would be very simple.
>
If you can use <sstream> you should prefer it. Stringstreams are likely the
most general and efficient way to do such conversions with C/C++. If you know
more about the input, you can optimize of course.
Ciao
Sebastian
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Lexical conversion.
@ 2002-10-16 6:03 Moore, Mathew L
2002-10-16 12:20 ` Sebastian Huber
0 siblings, 1 reply; 5+ messages in thread
From: Moore, Mathew L @ 2002-10-16 6:03 UTC (permalink / raw)
To: 'John Carter', Claudio Bley; +Cc: gcc-help
> > #include <sstream>
> >
> > string convert (const int number) {
> > ostringstream ostr;
> > ostr << number;
> > return ostr.str ();
> > }
>
> Sigh! I decided (especially as most the cases I worry about
> are in 0-9) to
> implement it like so.. (I unit tested it so I'm reasonable
> confident it
> works and its about 3 times faster in the general case than
> using printf)
Why do you suppose there is such a great speed improvement with the code
below? It seems to me that snprintf() should be able to optimize this task
quite effectively. The only overhead should be the parsing of the format
string, which in this case would be very simple.
--Matt
>
> string convert(const int value) {
> int abs_value;
>
> if (value >= 0) {
> // Optimise for single digit case...
> if (value <= 9) {
> return string( 1, '0'+value);
> }
>
> abs_value = value;
> } else {
> abs_value = -value;
> }
>
> char buf[100];
> char * start = buf+100;
> char * end = start;
>
> do {
> *(--start) = '0' + abs_value % 10;
> abs_value /= 10;
> } while (abs_value);
>
> if( value < 0) {
> *(--start) = '-';
> }
>
> return string(start, end-start);
> }
>
>
> --
>
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz
> New Zealand
>
> Good Ideas:
> Ruby - http://www.ruby-lang-org - The best of
> perl,python,scheme without the pain.
> Valgrind - http://developer.kde.org/~sewardj/ -
> memory debugger for x86-GNU/Linux
> Free your books - http://www.bookcrossing.com
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-10-16 19:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-14 20:29 Lexical conversion John Carter
2002-10-15 1:50 ` Claudio Bley
2002-10-15 17:14 ` John Carter
2002-10-16 6:03 Moore, Mathew L
2002-10-16 12:20 ` Sebastian Huber
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).