On 3 September 2015 at 22:17, Andres Freund <and...@anarazel.de> wrote:

> On 2015-09-03 16:28:40 +1200, David Rowley wrote:
> > I experimented with finding the fastest way to convert an int to a string
> > at the weekend, I did happen to be testing with int64's but likely int32
> > will be the same.
> >
> > This is the time taken to convert 30 into "30" 2 billion times.
> >
> > The closest implementation I'm using in the patch is pg_int64tostr() one.
> > The pg_int64tostr_memcpy() only performs better with bigger numbers /
> > longer strings.
> >
> > david@ubuntu:~/C$ gcc5.2 tostring.c -o tostring -O2
> > david@ubuntu:~/C$ ./tostring
> > pg_int64tostr_memcpy in 13.653252 seconds
> > pg_int64tostr in 2.042616 seconds
> > pg_int64tostr_new in 2.056688 seconds
> > pg_lltoa in 13.604653 seconds
> >
> > david@ubuntu:~/C$ clang tostring.c -o tostring_clang -O2
> > david@ubuntu:~/C$ ./tostring_clang
> > pg_int64tostr_memcpy in 0.000004 seconds
> > pg_int64tostr in 2.063335 seconds
> > pg_int64tostr_new in 2.102068 seconds
> > pg_lltoa in 3.424630 seconds
>
> Are you sure this isn't an optimization artifact where the actual work
> is optimized away? Doing 2 billion conversions in 2s kinda implies that
> the string conversion is done in ~4 cycles. That seems unrealistically
> fast, even for a pipelined cpu.
>

I think you're right.
If I change the NUM_TO_STRING in my tostring.c to
 #define NUM_TO_PRINT i%100

I just wanted a way to keep the strings 2 digits long, so that it matches
what will happen when building timestamps.

It looks a bit different, but it's still a bit faster than pg_lltoa()

david@ubuntu:~/C$ ./tostring
pg_int64tostr_memcpy in 16.557578 seconds
pg_int64tostr in 8.070706 seconds
pg_int64tostr_new in 7.597320 seconds
pg_lltoa in 12.315339 seconds

david@ubuntu:~/C$ ./tostring_clang
pg_int64tostr_memcpy in 19.698807 seconds
pg_int64tostr in 12.800270 seconds
pg_int64tostr_new in 14.174052 seconds
pg_lltoa in 14.427803 seconds

Regards

David Rowley

--
 David Rowley                   http://www.2ndQuadrant.com/
<http://www.2ndquadrant.com/>
 PostgreSQL Development, 24x7 Support, Training & Services

Reply via email to