public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: John Carter <john.carter@tait.co.nz>
To: Claudio Bley <bley@cs.uni-magdeburg.de>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Lexical conversion.
Date: Tue, 15 Oct 2002 17:14:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.44.0210161310200.21270-100000@parore> (raw)
In-Reply-To: <15787.54985.130530.545139@wh2-19.st.uni-magdeburg.de>

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

  reply	other threads:[~2002-10-16  0:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-14 20:29 John Carter
2002-10-15  1:50 ` Claudio Bley
2002-10-15 17:14   ` John Carter [this message]
2002-10-16  6:03 Moore, Mathew L
2002-10-16 12:20 ` Sebastian Huber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0210161310200.21270-100000@parore \
    --to=john.carter@tait.co.nz \
    --cc=bley@cs.uni-magdeburg.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).