[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: Code "optimization" (?)



On 25.01.2005 23:50, The Fox wrote:
> I was just looking into the code and I came across a function itoa 
> (src/common/misc.cpp) which converts decimal number to a string. Once 
> upon a time I even used it in my game (!). Some time ago I realized 
> that it could be written as:
> 
> #include <stdio.h>
> 
> char *itoa(int n, char *buf) {
> 	sprintf(buf, "%d, n);
> 	return buf;
> }
> 
> with (I think) the same functionality. As I'm not that experienced 
> programmer I'd like to know which way is faster/smaller/better. This 
> way you have to include the stdio and it's faster and easier to write. 
> Your way might be faster and/or smaller (when compiled) but I'm not 
> sure. What's your opinion?

Yes, it does the same. As to which one is faster/smaller/better, I
haven't actually tried it, but I would speculate that the code
produced by your variant is shorter (because it basically boils down
to one function call to an external library), but that the current
implementation is faster (because printf and friends are generally
quite expensive). Which one is better depends on what you want, I
guess. For our purposes, it doesn't really matter either way.

Jens