🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #42638

Re: performance measurements across 8 platforms: atoi()/sprintf("%d")

#42638
From: D. Dale Gulledge
Date: Sat, 18 Nov 1995 00:00
50 lines
1960 bytes
In article <DI229s.AA7@beaver.cs.washington.edu> forman@cs.washington.edu University of Washington, Computer Science & Engineering (George Forman) writes:

	 sprintf( buf, "%d", i);

	 fast_itoa( buf, i);

This is not a fair comparison.  You don't adhere to the same interface.
First, sprintf takes three arguments rather than two to accomplish the same
task.  That adds a couple of clock cycles.  Second, sprintf must parse the
second argument to determine what to do with the rest of its arguments.
Third, it must use varargs to get the third through nth arguments passed to
it.

   int  fast_itoa(char *buf, int i) /* fast version of sprintf(buf,"%d",i); */
   { 
       int nr_written=0;
       char tmp[40];
       char *pointer;
       pointer=tmp;
       *pointer++=0;

       if (i<0)
       {
	   i=-i;
	   *buf++='-';
	   nr_written++;
       }

       while (i)
       {
	   nr_written++;
	   *pointer++=(i%10)+48;
	   i/=10;
       }
       pointer--;

       while (*buf++=*pointer--);    /* copy number (reversed) from tmp to buf */
       return nr_written;
   }

Now for the bugs.  Try passing INT_MIN as the second argument.  On twos
complement machines it will fail.  Try passed a null pointer as the first
argument.

- Dale
--
D. Dale Gulledge, Software Engineer
Directory & Operator Services, Northern Telecom.

Warning: Bicyclist in mirror is moving faster than you think.
Technical Details
Message-ID: <gnusenetDDG.95Nov15170353@sun86.cci.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <DI229s.AA7@beaver.cs.washington.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,41bf6a7c37c19df3,start
X-Google-Attributes: gid11318c,public
From: ddg@cci.COM (D. Dale Gulledge)
Subject: Re: performance measurements across 8 platforms: atoi()/sprintf("%d")
Date: 1995/11/18
Message-ID: <gnusenetDDG.95Nov15170353@sun86.cci.com>
X-Deja-AN: 119671916
distribution: gnu
sender: daemon@cis.ohio-state.edu
references: <DI229s.AA7@beaver.cs.washington.edu>
organization: Northern Telecom Inc., D&OS
newsgroups: gnu.gcc.help