Giter Site home page Giter Site logo

jeaiii / itoa Goto Github PK

View Code? Open in Web Editor NEW
206.0 11.0 10.0 1.03 MB

Fast integer to ascii / integer to string conversion

License: MIT License

C++ 100.00%
itoa integer-conversion performance cpp ascii string-conversion single-header zero-dependency header-only

itoa's Introduction

itoa - Fast integer to ascii / integer to string conversion

windows

  • newest version: jeaiii_to_text.h
  • 0 dependency header only - as in absolutely no dependencies on ANY other headers
  • fast - fasted tested on vs2019 in x86 and x64 for all digit lengths, uniform random bits, and uniform random digit lengths (latest version is even 25% faster than before) using https://github.com/jeaiii/itoa-benchmark
  • small - simple code that produces good compiled code even in debug builds (no nested functions)
  • compiles on clang, msvc, and gcc as C++11 or later
  • written to specialize well based on integer type (size, signed, unsigned) and possible values determined at compile time
  • Thanks to Junekey Jeon for the great write-up here, providing better insight into more rigorous mathematical foundations which eliminated an unneeded addition, and renewing my interest into making an even faster version

Check it out vs. {fmt} on godbolt.org

Disclaimer: This is not a good way to benchmark. It's not very repeatable and google benchmark is compiled in debug, although fmt being mostly header is optimized and inlined well. The performance charts below are much more accurate.

-----------------------------------------------------
Benchmark           Time             CPU   Iterations
-----------------------------------------------------
BM_fmt           16.6 ns         10.5 ns     75504520
BM_jea           10.2 ns         4.13 ns    159688774

The last column is uniform random length numbers from 1 to N digits - worst case for branch prediction based on number of digits

u32toa_chart

jeaiii vs. fmt

itoa's People

Contributors

jeaiii avatar syoyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

itoa's Issues

warnings emitted by msvc

When compiling with MSVC I'm getting warnings:
jeaiii_to_text.h(147): warning C4305: '/=': truncation from 'jeaiii::u32' to 'U'
jeaiii_to_text.h(209): warning C4305: '/=': truncation from 'jeaiii::u32' to 'U'

Replacing

n /= u32(1e8);

by

n = U(n /u32(1e8));

solves it.

Clarify roles of the two implementations

Just found this library and am quite surprised by how much of a performance improvement it made for our project!

However, while the interface of the functions is indeed so simple as to be mostly self explanatory, I am unsure about the roles of itoa_jeaiii.cpp and to_chars.cpp, i.e. which one to choose when, seeing that they basically offer the same functionality...

Thanks!

Features: zero-padding and fixed-point

I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.

The other use-case is slightly less common but it can be often helpful to represent a fixed-point decimal number with a standard integer type like int32_t or int64_t where the number is stored as N * 10^D where D is the number of decimal digits supported. I was trying to think of how one could utilize your same approach but with the added issue of inserting the decimal place and potentially having leading zeros after the decimal but before the first digit.

Finally there is the fixed length, fixed-point decimal number, where you want to render the same number fixed-point decimal number but with a fixed number of decimal places and zero padding in the front for the rest

Mostly just thoughts on how this could be extended, I could provide naïve solutions to each of these if its helpful

Versions for uint8_t, int8_t, uint16_t and int16_t

This is a fantastic, really fast algorithm, that even seems to beat Vitaut's. Many thanks for making this available.

My issue is actually a feature request. For completion, do you think that beyond the currently available implementations for uint32_t, int32_t, uint64_t and int64_t, would it be possible to add, in the future, implementations also for uint8_t, int8_t, uint16_t and int16_t?

It would be amazing to have those, for really fast conversion of low-latency batches of data.

GCC warning due to ASCII art

The ASCII art on "to_text_from_integer.h" generates a compiler warning with GCC 8:

itoa/to_text_from_integer.h:39:1: warning: multi-line comment [-Wcomment]
 //    /\   \     \     \     \
 ^

If you comment that line on the header file you also get a warning on the next one. I think it is due to the use of the character "\".

Clang missing-braces

There is an warning from Clang 6.

itoa_jeaiii.cpp:65:35: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
static const pair s_pairs[] = { P('0'), P('1'), P('2'), P('3'), P('4'), P('5'), P('6'), P('7'), P('8'), P('9') };
                                ~~^~~~
                                {
itoa_jeaiii.cpp:64:14: note: expanded from macro 'P'
#define P(T) T, '0',  T, '1', T, '2', T, '3', T, '4', T, '5', T, '6', T, '7', T, '8', T, '9'
             ^~~~~~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.