A faster and more elegant implementation:
Code:
inline int inttostr( char *s, int n )
{
unsigned int i = 1000000000;
/* largest base 10 'place' < 2^32 */
/* for a 64 bit int - 10000000000000000000 */
/* for a 16 bit int - 10000 */
/* etc */
/* if we are signed, put the sign in the string;
afterwards, it doesn't matter */
if( ((signed)n) < 0 ) {
*s++ = '-';
n = 0 - n;
}
do
*s++ = '0' + (((n-n%i)/i)%10);
while( i /= 10 );
/* terminating null */
*s = 0;
/* return value should always be zero, unless
there was some unpredicted badness */
return n;
}
comparison:
one million reps of this:
Code:
real 0m0.198s
user 0m0.188s
sys 0m0.000s
vs one million reps of above
Code:
real 0m0.228s
user 0m0.224s
sys 0m0.000s