public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* 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
* 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

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-16  6:03 Lexical conversion Moore, Mathew L
2002-10-16 12:20 ` Sebastian Huber
  -- strict thread matches above, loose matches on Subject: below --
2002-10-14 20:29 John Carter
2002-10-15  1:50 ` Claudio Bley
2002-10-15 17:14   ` John Carter

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).