JiiPee <
n...@notvalid.com> wrote in news:yEf5w.641279$O%1.13...@fx31.am4:
> On 02/11/2014 00:55, Luca Risolia wrote:
>> Il 01/11/2014 23:02, JiiPee ha scritto:
>>
>>> For me, I already added that new fast conversion function to my
>>> toolset... I rather use it than library versions. btw if somebody
>>> wants my fast function please ask, I can give it then. Also, if
>>> somebody knows faster way to convert, am ready to test it against
>>> this! I have a test platform ready.
>>
>> And how can we reproduce your tests? Please post the full, compilable
>> test program together with all the informations about the compiler,
>> compiler version and compilation flags that you used.
>>
>
> I also tested intToStr that it converts correctly: first 20 million is
> correct. Needs to test all integers, takes 2 hours.... gonna do
> sometimes.
>
> compiler: GCC 4.8.1, use C++11, -O3, -s
It appears that the crux of the fast algorithm is to have some static
lookup buffer (digit_pairs) which encodes the ASCII representation of
numbers 0..99. (You forgot to include that, copied it from the
stackoverflow page). So, it trades some memory for speed. So far, so
good.
For fast operation it is essential the digit_pairs array is in the cache.
In your test, this is most probably the case, but in real code the int-
to-string conversions probably happen much less often, so the performance
would not be so great in the real code. Standard library implementations
probably operate only with data in registers so the cache access is not
an issue.
Another point is that in C++ you want to wrap the result in a std::string
or something, and that will also slow things down. In my test runs
wrapping time was in the same range than the conversion itself and if
std::string happens to not use SSO then I guess it may be even much
slower.
So, it is not really clear how much would the win be in real programs.
Anyway, here are my test results for running your test program (MSVC2012,
Release x64 mode):
Using _snprintf, no wrapping: 4.622 s
Using intToStr, no wrapping: 1.143 s (4.0 times faster)
Using _snprintf, wrapping to a std::string: 6.622 s
Using boost::lexical_cast<std::string>: 8.75 s
Using intToStr, wrapping to a std::string: 2.996 s (2.2 times faster than
_snprintf and 2.9 times faster than boost::lexical_cast).
So, here the differences are around 2-3 times and probably smaller in the
real code when the lookup array is out of the cache. Not sure if this
justifies optimizing something which is already quite fast anyway.
OTOH, if there are indeed differences like 130 times on some platform,
then I guess the standard library implementation must be quite bad
indeed.
Cheers
Paavo