Copyright(c) 2016 Arturo Martin-de-Nicolas ([email protected]) See LICENSE file for source code copyrights.
Fast and simple integer to ASCII conversion:
- 32 and 64-bit integers
- signed and unsigned
- user supplied buffer must be large enough for all decimal digits in value plus minus sign if negative
- left-justified
- NUL terminated
- return value is pointer to NUL terminator
- for best performance, compile with -O3 optimization level
char* itoa(uint32_t u, char* buffer);
char* itoa( int32_t i, char* buffer);
char* itoa(uint64_t u, char* buffer);
char* itoa( int64_t i, char* buffer);
- itoa_ljust.h - programming interface
- itoa_ljust.cpp - implementation - compile and link with your application
This implemenation is incorporated in Milo Yip's excellent itoa-benchmark - it is called amartin there.
The benchmark uses sprintf as the baseline and compares various implementations running both sequential and random integer sequences for signed and unsigned integers of 32-bit and 64-bit sizes (8 different tests).
When compiled with gcc 5.3 at -O3 optimization level and run on an Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz this implementation is the fastest in all categories (the null function does nothing, used to measure overhead of benchmark loop).
Function | Time (ns) | Speedup |
---|---|---|
sprintf | 67.161 | 1.00x |
mwilson | 29.509 | 2.28x |
naive | 28.426 | 2.36x |
unnamed | 25.490 | 2.63x |
count | 24.523 | 2.74x |
lut | 22.142 | 3.03x |
countlut | 20.377 | 3.30x |
sse2 | 20.201 | 3.32x |
branchlut | 19.098 | 3.52x |
amartin | 16.845 | 3.99x |
null | 1.424 | 47.18x |
Function | Time (ns) | Speedup |
---|---|---|
sprintf | 54.168 | 1.00x |
mwilson | 17.785 | 3.05x |
naive | 16.507 | 3.28x |
unnamed | 14.151 | 3.83x |
count | 13.491 | 4.02x |
lut | 11.945 | 4.53x |
countlut | 8.403 | 6.45x |
branchlut | 8.028 | 6.75x |
sse2 | 6.261 | 8.65x |
amartin | 6.106 | 8.87x |
null | 1.421 | 38.13x |