Il 16/11/2014 15:10, JiiPee ha scritto:
> On 16/11/2014 13:34, Paavo Helde wrote:
>> JiiPee <
n...@notvalid.com> wrote in
>> news:zUH9w.1104999$Fy6.9...@fx25.am4:
>>
>>> On 15/11/2014 09:26, Jorgen Grahn wrote:
>>>> Side note: it's better in cases like this to put it on github or
>>>> something, and here just mention which version you're talking about
>>>> -- since you're going to change the code, and others will too.
>>>>
>>> That was a good practice for me to improve that intToString function,
>>> although Luca has even faster version so best to use that (dont
>>> understand that code though).
>> Luca's code is also trading memory for speed, but this time it is the
>> compiled code size (in my experimentation Luca's version compiled into
>> 2564
>> bytes .o and your version with 200-byte lookup table compiled into 1328
>> bytes. And in my tests it was slower, not faster. Probably I had too old
>> gcc version. Or it was something else.
>>
>> Cheers
>> Paavo
>>
>>
>
> His version was slower than my first version? So the Superfast would be
> even faster? I think I ll do more tests of other computers.
Test the function below. It is equivalent to the code that the compiler
should be able to generate from my original (and more generic) version
on implementations where unsigned integer is 32-bit long.
void uint_to_str(unsigned int i, char* c) {
if (i < 10) {
c[0] = '0' + (i % 10);
c[1] = '\0';
} else if (i < 100) {
c[0] = '0' + (i / 10);
c[1] = '0' + (i % 10);
c[2] = '\0';
} else if (i < 1000) {
c[0] = '0' + (i / 100);
c[1] = '0' + (i % 100) / 10;
c[2] = '0' + (i % 10);
c[3] = '\0';
} else if (i < 10000) {
c[0] = '0' + (i / 1000);
c[1] = '0' + (i % 1000) / 100;
c[2] = '0' + (i % 100) / 10;
c[3] = '0' + (i % 10);
c[4] = '\0';
} else if (i < 100000) {
c[0] = '0' + (i / 10000);
c[1] = '0' + (i % 10000) / 1000;
c[2] = '0' + (i % 1000) / 100;
c[3] = '0' + (i % 100) / 10;
c[4] = '0' + (i % 10);
c[5] = '\0';
} else if (i < 1000000) {
c[0] = '0' + (i / 100000);
c[1] = '0' + (i % 100000) / 10000;
c[2] = '0' + (i % 10000) / 1000;
c[3] = '0' + (i % 1000) / 100;
c[4] = '0' + (i % 100) / 10;
c[5] = '0' + (i % 10);
c[6] = '\0';
} else if (i < 10000000) {
c[0] = '0' + (i / 1000000);
c[1] = '0' + (i % 1000000) / 100000;
c[2] = '0' + (i % 100000) / 10000;
c[3] = '0' + (i % 10000) / 1000;
c[4] = '0' + (i % 1000) / 100;
c[5] = '0' + (i % 100) / 10;
c[6] = '0' + (i % 10);
c[7] = '\0';
} else if (i < 100000000) {
c[0] = '0' + (i / 10000000);
c[1] = '0' + (i % 10000000) / 1000000;
c[2] = '0' + (i % 1000000) / 100000;
c[3] = '0' + (i % 100000) / 10000;
c[4] = '0' + (i % 10000) / 1000;
c[5] = '0' + (i % 1000) / 100;
c[6] = '0' + (i % 100) / 10;
c[7] = '0' + (i % 10);
c[8] = '\0';
} else if (i < 1000000000) {
c[0] = '0' + (i / 100000000);
c[1] = '0' + (i % 100000000) / 10000000;
c[2] = '0' + (i % 10000000) / 1000000;
c[3] = '0' + (i % 1000000) / 100000;
c[4] = '0' + (i % 100000) / 10000;
c[5] = '0' + (i % 10000) / 1000;
c[6] = '0' + (i % 1000) / 100;
c[7] = '0' + (i % 100) / 10;
c[8] = '0' + (i % 10);
c[9] = '\0';
} else {
c[0] = '0' + (i / 1000000000);
c[1] = '0' + (i % 1000000000) / 100000000;
c[2] = '0' + (i % 100000000) / 10000000;
c[3] = '0' + (i % 10000000) / 1000000;
c[4] = '0' + (i % 1000000) / 100000;
c[5] = '0' + (i % 100000) / 10000;
c[6] = '0' + (i % 10000) / 1000;
c[7] = '0' + (i % 1000) / 100;
c[8] = '0' + (i % 100) / 10;
c[9] = '0' + (i % 10);
c[10] = '\0';
}
}