From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4114 invoked by alias); 16 Oct 2002 00:14:54 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 4107 invoked from network); 16 Oct 2002 00:14:53 -0000 Received: from unknown (HELO gatekeeper.tait.co.nz) (202.37.96.11) by sources.redhat.com with SMTP; 16 Oct 2002 00:14:53 -0000 Received: from gatekeeper.tait.co.nz (localhost.localdomain [127.0.0.1]) by gatekeeper.tait.co.nz (8.11.2/8.9.3) with ESMTP id g9G0EqF15569 for ; Wed, 16 Oct 2002 13:14:52 +1300 Received: from sunstorm.tait.co.nz (sunstorm.tait.co.nz [172.25.40.9]) by gatekeeper.tait.co.nz (8.11.2/8.9.3) with ESMTP id g9G0EqY15560; Wed, 16 Oct 2002 13:14:52 +1300 Received: from parore (parore.tait.co.nz [172.25.140.12]) by sunstorm.tait.co.nz (iPlanet Messaging Server 5.1 (built May 7 2001)) with ESMTP id <0H4100AEWSOSPE@sunstorm.tait.co.nz>; Wed, 16 Oct 2002 13:14:52 +1300 (NZDT) Date: Tue, 15 Oct 2002 17:14:00 -0000 From: John Carter Subject: Re: Lexical conversion. In-reply-to: <15787.54985.130530.545139@wh2-19.st.uni-magdeburg.de> X-Originating-IP: 127.0.0.1 X-X-Sender: johnc@parore To: Claudio Bley Cc: gcc-help@gcc.gnu.org Message-id: X-Complaints-to: /dev/null MIME-version: 1.0 Content-type: TEXT/PLAIN; charset=US-ASCII Content-transfer-encoding: 7BIT X-Priority: 4294967296 X-Apparently-From: mars X-Contents: May contain traces of nuts. X-SW-Source: 2002-10/txt/msg00199.txt.bz2 On Tue, 15 Oct 2002, Claudio Bley wrote: > >>>>> "John" == John Carter 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 > > 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