{"id":18977600,"url":"https://github.com/ibireme/c_numconv_benchmark","last_synced_at":"2025-06-11T04:09:20.279Z","repository":{"id":50318226,"uuid":"304050276","full_name":"ibireme/c_numconv_benchmark","owner":"ibireme","description":"C/C++ number ↔︎ string benchmark","archived":false,"fork":false,"pushed_at":"2025-03-06T17:56:35.000Z","size":4281,"stargazers_count":62,"open_issues_count":0,"forks_count":7,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-09T00:06:15.576Z","etag":null,"topics":["atoi","c","conversion","cpp","dtoa","itoa","number","performance","strtod"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ibireme.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-14T15:02:03.000Z","updated_at":"2025-04-24T08:56:39.000Z","dependencies_parsed_at":"2024-08-26T20:15:07.373Z","dependency_job_id":"915cba80-3b51-4547-80c0-6811aab2b813","html_url":"https://github.com/ibireme/c_numconv_benchmark","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibireme%2Fc_numconv_benchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibireme%2Fc_numconv_benchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibireme%2Fc_numconv_benchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibireme%2Fc_numconv_benchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ibireme","download_url":"https://codeload.github.com/ibireme/c_numconv_benchmark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166518,"owners_count":21864476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atoi","c","conversion","cpp","dtoa","itoa","number","performance","strtod"],"created_at":"2024-11-08T15:29:44.329Z","updated_at":"2025-05-09T00:06:22.216Z","avatar_url":"https://github.com/ibireme.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Number Conversion Benchmark in C\nThis project evaluates the performance of functions that convert numbers to and from strings in C/C++.\n\nThe functions named `yy` is implemented by me, and used in the [yyjson](https://github.com/ibireme/yyjson) library.\n\n# Requirement\n\n- A modern compiler or IDE supporting C11 and C++17.\n- CMake 3.14+ for building this project.\n- Git for interacting with the submodule in this repository.\n\n# Building\n\nClone this repository and initialize submodules:\n```shell\ngit clone https://github.com/ibireme/c_numconv_benchmark.git\ncd c_numconv_benchmark\ngit submodule update --init\n```\n\nBuild and run:\n```shell\nmkdir build\ncd build\ncmake ..\ncmake --build . --config Release\n./run_itoa -o report_itoa.html\n./run_atoi -o report_atoi.html\n./run_dtoa -o report_dtoa.html\n./run_strtod -o report_strtod.html\n```\n\nBuild with other compiler or IDE:\n```shell\n# Use arch native:\ncmake .. -DARCH_NATIVE=ON\n\n# Clang for Linux/Unix:\ncmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++\n\n# Microsoft Visual Studio for Windows:\ncmake .. -G \"Visual Studio 16 2019\" -A x64\ncmake .. -G \"Visual Studio 16 2019\" -A Win32\n\n# Xcode for macOS:\ncmake .. -G Xcode\n```\n\n## Make the results more stable and accurate\n\nThis benchmark project uses `cpu cycle` for measurement, so `Turbo Boost` and similar technologies should be disabled to make the result more stable and accurate.\n\n\n# Functions\n\n-------\n\n### Integer to String (itoa)\nFunction prototype:\n```c\nchar *itoa_u32(uint32_t val, char *buf);\nchar *itoa_i32(int32_t val, char *buf);\nchar *itoa_u64(uint64_t val, char *buf);\nchar *itoa_i64(int64_t val, char *buf);\n```\n![img](docs/images/itoa-u64-fixed-length.png)\n![img](docs/images/itoa-u64-random-length.png)\n\nClick these links to see more reports with interactive charts:\n* [clang_apple_m1](https://ibireme.github.io/c_numconv_benchmark/reports/clang_apple_m1/itoa.html)\n* [clang_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/clang_intel_i5_13600/itoa.html)\n* [gcc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/gcc_intel_i5_13600/itoa.html)\n* [msvc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/msvc_intel_i5_13600/itoa.html)\n\n\n-------\n\n### String to Integer (atoi)\nFunction prototype:\n```c\n// Function prototype:\ntypedef enum {\n    atoi_result_suc = 0,\n    atoi_result_fail = 1,\n    atoi_result_overflow = 2,\n} atoi_result;\n\nuint32_t atoi_u32(const char *str, size_t len, char **endptr, atoi_result *res);\nint32_t atoi_i32(const char *str, size_t len, char **endptr, atoi_result *res);\nuint64_t atoi_u64(const char *str, size_t len, char **endptr, atoi_result *res);\nint64_t atoi_i64(const char *str, size_t len, char **endptr, atoi_result *res);\n```\n\n![img](docs/images/atoi-i64-fixed-length.png)\n![img](docs/images/atoi-i64-random-length.png)\n\nClick these links to see more reports with interactive charts:\n* [clang_apple_m1](https://ibireme.github.io/c_numconv_benchmark/reports/clang_apple_m1/atoi.html)\n* [clang_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/clang_intel_i5_13600/atoi.html)\n* [gcc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/gcc_intel_i5_13600/atoi.html)\n* [msvc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/msvc_intel_i5_13600/atoi.html)\n\n-------\n\n### Double to String (dtoa)\nFunction prototype:\n```c\n// Function prototype:\nchar *dtoa(double val, char *buf);\n```\n![img](docs/images/dtoa-fixed-length.png)\n![img](docs/images/dtoa-nomalized.png)\n![img](docs/images/dtoa-integer.png)\n\nClick these links to see more reports with interactive charts:\n* [clang_apple_m1](https://ibireme.github.io/c_numconv_benchmark/reports/clang_apple_m1/dtoa.html)\n* [clang_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/clang_intel_i5_13600/dtoa.html)\n* [gcc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/gcc_intel_i5_13600/dtoa.html)\n* [msvc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/msvc_intel_i5_13600/dtoa.html)\n\nNote: the following functions may not generate shortest decimal representation, or may not remove the trailing zeros in fraction part:\n* fpconv\n* milo\n* emyg\n* erthink\n\nThe following functions require SSE2 or AVX512, use `cmake .. -DARCH_NATIVE=ON` to enable them:\n* xjb_sse2\n* xjb_avx512\n\n-------\n\n### String to Double (strtod)\nFunction prototype:\n```c\n// Function prototype:\ndouble strtod(const char *str, size_t len, char **endptr);\n```\n![img](docs/images/strtod-fixed-length.png)\n![img](docs/images/strtod-random-normalized.png)\n![img](docs/images/strtod-integer-fixed-len.png)\n\n\nClick these links to see more reports with interactive charts:\n* [clang_apple_m1](https://ibireme.github.io/c_numconv_benchmark/reports/clang_apple_m1/strtod.html)\n* [clang_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/clang_intel_i5_13600/strtod.html)\n* [gcc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/gcc_intel_i5_13600/strtod.html)\n* [msvc_intel_i5_13600](https://ibireme.github.io/c_numconv_benchmark/reports/msvc_intel_i5_13600/strtod.html)\n\nNote: \n* `yy_fast` may returns inaccurate result with 0-2 ulp error in some cases.\n* `ryu` and `lemire` may reject some integer numbers or large numbers.\n\n-------\n\n# Referenced libraries and articles\n\n**Google (double \u003c-\u003e string)**\n\nEfficient binary-decimal and decimal-binary conversion routines for IEEE doubles.\n\n- \u003chttps://github.com/google/double-conversion\u003e (C++ library)\n\n**Swift (double -\u003e string)**\n\nConvert double to string quickly and accurately in SwiftLang.\n\n- \u003chttps://github.com/swiftlang/swift/blob/main/stdlib/public/runtime/SwiftDtoa.cpp\u003e (C code)\n- \u003chttps://github.com/swiftlang/swift/pull/16178\u003e (V1 discussion)\n- \u003chttps://github.com/swiftlang/swift/pull/35299\u003e (V2 discussion)\n\n**David Gay (double \u003c-\u003e string)**\n\nWidely used ANSI C implementations of strtod and dtoa.\n\n- \u003chttp://www.netlib.org/fp/\u003e (C code)\n\n**fmtlib (double -\u003e string, integer -\u003e string)**\n\nA modern formatting library for C++.\n\n- \u003chttps://github.com/fmtlib/fmt\u003e (C++ library)\n- \u003chttps://github.com/fmtlib/format-benchmark\u003e (Benchmark)\n\n**abseil (double \u003c-\u003e string, integer \u003c-\u003e string)**\n\nA C++ Common Libraries.\n\n- \u003chttps://github.com/abseil/abseil-cpp\u003e (C++ library)\n\n**ryu (double \u003c-\u003e string)**\n\nConverts floating point numbers to decimal strings.\n\n- \u003chttps://github.com/ulfjack/ryu\u003e (C library)\n- \u003chttps://www.youtube.com/watch?v=kw-U6smcLzk\u003e (Talk)\n- \u003chttps://dl.acm.org/doi/10.1145/3192366.3192369\u003e (Paper)\n\n**Schubfach (doube -\u003e string)**\n\nThe Schubfach way to render doubles by Raffaello Giulietti.\n     \n- \u003chttps://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb\u003e (Paper)\n- \u003chttps://github.com/openjdk/jdk/pull/3402\u003e (Java implementation)\n- \u003chttps://github.com/abolz/Drachennest\u003e (C++ implementation)\n\n**dragonbox and grisu-exact (double -\u003e string)**\n\nImplementation of Dragonbox in C++.\n\n- \u003chttps://github.com/jk-jeon/dragonbox\u003e (C++ library and paper)\n- \u003chttps://github.com/jk-jeon/Grisu-Exact\u003e (C++ library)\n\n**fast_double_parser (string -\u003e double)**\n\nFast function to parse strings into double by Daniel Lemire.\n\n- \u003chttps://github.com/lemire/fast_double_parser\u003e (C++ library)\n- \u003chttps://github.com/fastfloat/fast_float\u003e (C++ library)\n- \u003chttps://www.youtube.com/watch?v=AVXgvlMeIm4\u003e (Talk)\n- \u003chttps://arxiv.org/abs/2101.11408\u003e (Paper)\n\n**itoa benchmark (integer -\u003e string)**\n\nC++ integer-to-string conversion benchmark\n- \u003chttps://github.com/miloyip/itoa-benchmark\u003e (Benchmark)\n\n**dtoa benchmark (double -\u003e string)**\n\nC++ double-to-string conversion benchmark\n\n- \u003chttps://github.com/miloyip/dtoa-benchmark\u003e (Benchmark)\n\n**AppNexus (integer -\u003e string)**\n\nPrint integers faster\n\n- \u003chttps://www.reddit.com/r/programming/comments/7ljzty/how_to_print_integers_really_fast_with_source_code/\u003e (Discussion)\n- \u003chttps://medium.com/xandr-tech/appnexus-common-framework-its-out-also-how-to-print-integers-faster-ceb72ac171a1\u003e (Blog)\n- \u003chttps://github.com/appnexus/acf/blob/master/src/an_itoa.c\u003e (Code)\n\n**benchmark (string -\u003e integer)**\n\n- \u003chttps://medium.com/@julienjorge/benchmarking-atoui-a-follow-up-to-writing-fast-code-90e722590f4d\u003e (Blog)\n- \u003chttps://github.com/j-jorge/atoi-benchmark\u003e (Benchmark)\n\n**benchmark (double -\u003e string, integer -\u003e string)**\n\n- \u003chttps://github.com/miloyip/dtoa-benchmark\u003e (Benchmark)\n- \u003chttps://github.com/miloyip/itoa-benchmark\u003e (Benchmark)\n\n\n\n\n\n\n# License\nThis project is distributed under the MIT license.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibireme%2Fc_numconv_benchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fibireme%2Fc_numconv_benchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibireme%2Fc_numconv_benchmark/lists"}