您的位置:首页 > 其它

itoa的具体实现、测试和优化

2009-12-01 18:26 141 查看
今天看到有人在百度知道提问:要写一个itoa的函数(仿c语言的)。看了一下,以前做过,也就随手写了。我把代码发出来,献丑了。
inline void myitoa(int value, char *str, int radio = 10)
{
char *p = str;
char *ptemp;
unsigned int temp = value;
char tempc;
unsigned int i;
if( value < 0 )
{
*p++ = '-';
temp = (unsigned)(-value);
}
ptemp = p;
do
{
i = temp % radio;
if(i < 10)
*p++ = (char)(i + '0');
else
*p++ = (char)(i - 10 + 'a');
temp = temp / radio;
} while( temp > 0 );
*p-- = '/0';
while( p > ptemp )
{
tempc = *p;
*p-- = *ptemp;
*ptemp++ = tempc;
}
}


写完,唉,突然想测试一下他的速度,下面是我的函数和c的itoa函数时间的对比截图:



哇,这么大的差距啊。不信。
开一下优化试一下,结果:



不错哦!但还是有很大的差距啊。
不行,作为程序员,是不能认输的。我上网找了一下。下面是两个比较好的代码。一个跟我的差不多。另一个却有一个很好的思想。来,测试一下他们的运行时间。



myitoa是我写的,下面两个是网上找的,最后一个是c库函数
哎,我自己写的函数跟他们比还是有差距啊。
但我不明白我的函数跟myxtoa的差不多,为什么他的运行时间就比我的要短一倍呢(在开优化的时候)? 不懂。
库函数的优化还真不是赖的。无论是否编译器是否开优化的情况下都能有一个很好的运行时间。看来以后能用库里的东西,就用库里的东西了。
继续思考怎么优化。(如果你有办法帮我的话,请给我留言哦!谢谢了!!!)
下面是另外两个itoa函数,在网上找的。我都附上了,希望对大家有帮助吧。

char   *my1itoa(   int   value,   char   *str,   int   radix   )
{
static   char   szMap[]   =   {   							//   字符映射表
'0',   '1',   '2',   '3',   '4',   '5',
'6',   '7',   '8',   '9',   'a',   'b',
'c',   'd',   'e',   'f',   'g',   'h',
'i',   'j',   'k',   'l',   'm',   'n',
'o',   'p',   'q',   'r',   's',   't',
'u',   'v',   'w',   'x',   'y',   'z'
};
int   nCount   =   -1,   nIndex;
char   *pStr   =   str,   nTemp;
if   (   radix   >=   2   &&   radix   <=   36   )    //   限制radix必须在2到36之间
{
if   (   value   <   0   &&   radix   ==   10   )    //   如果是负数就在首位添加负号,并将字符串前移
{
*pStr++   =   '-';
value   =   -value;   //转为正数,
}
unsigned   int   nValue   =   *(unsigned*)&value;
do   { 													//   循环转换每一个数字,直到结束
pStr[   ++nCount   ]   =   szMap[   nValue   %   radix   ];
nValue   /=   radix;
}   while(   nValue   >   0   );  						 //   转换结束后字符串是翻的
nIndex = ( nCount + 1 ) / 2;   //   计算出一半的长度
while(   nIndex--   >   0   )   {   //   将字符串的字符序翻转
nTemp   =   pStr[   nIndex   ];
pStr[   nIndex   ]   =   pStr[   nCount   -   nIndex   ];
pStr[   nCount   -   nIndex   ]   =   nTemp;
}
}
pStr[   nCount   +   1   ]   =   '/0';   //   置结束符
return   str;
}


static   void   __cdecl   myxtoa   (
unsigned   long   val,
char   *buf,
unsigned   radix,
int   is_neg
)
{
char   *p;                                 /*   pointer   to   traverse   string   */
char   *firstdig;                  		 /*   pointer   to   first   digit   */
char   temp;                            	 /*   temp   char   */
unsigned   digval;                		 /*   value   of   digit   */
p   =   buf;
if   (is_neg)   { 		/*   negative,   so   output   '-'   and   negate   */
*p++   =   '-';
val   =   (unsigned   long)(-(long)val);
}
firstdig   =   p;                      	 /*   save   pointer   to   first   digit   */
do{
digval   =   (unsigned)   (val   %   radix);
val   /=   radix;              	 /*   get   next   digit   */
/*   convert   to   ascii   and   store   */
if  (digval   >   9)
*p++ = (char)(digval - 10 + 'a');     /*   a   letter   */
else
*p++ = (char)(digval + '0');               /*   a   digit   */
} while (val > 0);

/*   We   now   have   the   digit   of   the   number   in   the   buffer,   but   in   reverse
order.     Thus   we   reverse   them   now.   */

*p--   =   '/0';                         /*   terminate   string;   p   points   to   last   digit   */

do   {
temp   =   *p;
*p   =   *firstdig;
*firstdig   =   temp;       /*   swap   *p   and   *firstdig   */
--p;
++firstdig;                   /*   advance   to   next   two   digits   */
}   while   (firstdig   <   p);   /*   repeat   until   halfway   */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐