From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8929 invoked by alias); 16 Oct 2002 13:03:44 -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 8922 invoked from network); 16 Oct 2002 13:03:43 -0000 Received: from unknown (HELO bclcl1.im.battelle.org) (131.167.1.2) by sources.redhat.com with SMTP; 16 Oct 2002 13:03:43 -0000 Received: from ns-bco-scn1.im.battelle.org ([131.167.1.122]) by BCLCL1 (PMDF V5.1-10 #U2779) with SMTP id <01KNQ6UC0N2K95RHKM@BCLCL1> for gcc-help@gcc.gnu.org; Wed, 16 Oct 2002 09:03:54 EDT Received: from 131.167.1.91 by ns-bco-scn1.im.battelle.org (InterScan E-Mail VirusWall NT); Wed, 16 Oct 2002 09:03:33 -0400 Received: by ws-bco-mse1.milky-way.battelle.org with Internet Mail Service (5.5.2655.55) id ; Wed, 16 Oct 2002 09:03:34 -0400 Date: Wed, 16 Oct 2002 06:03:00 -0000 From: "Moore, Mathew L" Subject: RE: Lexical conversion. To: 'John Carter' , Claudio Bley Cc: gcc-help@gcc.gnu.org Message-id: <2F05A390F72A0A409390E016D23E45E8042DBE44@ns-bco-mse4.im.battelle.org> MIME-version: 1.0 Content-type: text/plain; charset="iso-8859-1" X-SW-Source: 2002-10/txt/msg00200.txt.bz2 > > #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) 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 >