Skip to content

ibireme/c_numconv_benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Number Conversion Benchmark in C

This project evaluates the performance of functions that convert numbers to and from strings in C/C++.

The functions named yy is implemented by me, and used in the yyjson library.

Requirement

  • A modern compiler or IDE supporting C11 and C++17.
  • CMake 3.14+ for building this project.
  • Git for interacting with the submodule in this repository.

Building

Clone this repository and initialize submodules:

git clone https://github.com/ibireme/c_numconv_benchmark.git
cd c_numconv_benchmark
git submodule update --init

Build and run:

mkdir build
cd build
cmake ..
cmake --build . --config Release
./run_itoa -o report_itoa.html
./run_atoi -o report_atoi.html
./run_dtoa -o report_dtoa.html
./run_strtod -o report_strtod.html

If you want to build with other compiler or IDE, try these commands:

# Clang for Linux/Unix:
cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

# Microsoft Visual Studio for Windows:
cmake .. -G "Visual Studio 16 2019" -A x64
cmake .. -G "Visual Studio 16 2019" -A Win32

# Xcode for macOS:
cmake .. -G Xcode

Make the results more stable and accurate

This benchmark project uses cpu cycle for measurement, so Turbo Boost and similar technologies should be disabled to make the result more stable and accurate.

Functions


Integer to String (itoa)

Function prototype:

char *itoa_u32(uint32_t val, char *buf);
char *itoa_i32(int32_t val, char *buf);
char *itoa_u64(uint64_t val, char *buf);
char *itoa_i64(int64_t val, char *buf);

img img

Click these links to see more reports with interactive charts:


String to Integer (atoi)

Function prototype:

// Function prototype:
typedef enum {
    atoi_result_suc = 0,
    atoi_result_fail = 1,
    atoi_result_overflow = 2,
} atoi_result;

uint32_t atoi_u32(const char *str, size_t len, char **endptr, atoi_result *res);
int32_t atoi_i32(const char *str, size_t len, char **endptr, atoi_result *res);
uint64_t atoi_u64(const char *str, size_t len, char **endptr, atoi_result *res);
int64_t atoi_i64(const char *str, size_t len, char **endptr, atoi_result *res);

img img

Click these links to see more reports with interactive charts:


Double to String (dtoa)

Function prototype:

// Function prototype:
char *dtoa(double val, char *buf);

img img img

Click these links to see more reports with interactive charts:

Note: the following functions may not generate shortest decimal representation, or may not remove the trailing zeros in fraction part:

  • fpconv
  • milo
  • emyg
  • erthink

String to Double (strtod)

Function prototype:

// Function prototype:
double strtod(const char *str, size_t len, char **endptr);

img img img

Click these links to see more reports with interactive charts:

Note:

  • yy_fast may returns inaccurate result with 0-2 ulp error in some cases.
  • ryu and lemire may reject some integer numbers or large numbers.

Referenced libraries and articles

Google (double <-> string)

Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles.

Swift (double -> string)

Convert double to string quickly and accurately in SwiftLang.

David Gay (double <-> string)

Widely used ANSI C implementations of strtod and dtoa.

fmtlib (double -> string, integer -> string)

A modern formatting library for C++.

abseil (double <-> string, integer <-> string)

A C++ Common Libraries.

ryu (double <-> string)

Converts floating point numbers to decimal strings.

Schubfach (doube -> string)

The Schubfach way to render doubles by Raffaello Giulietti.

dragonbox and grisu-exact (double -> string)

Implementation of Dragonbox in C++.

fast_double_parser (string -> double)

Fast function to parse strings into double by Daniel Lemire.

itoa benchmark (integer -> string)

C++ integer-to-string conversion benchmark

dtoa benchmark (double -> string)

C++ double-to-string conversion benchmark

AppNexus (integer -> string)

Print integers faster

benchmark (string -> integer)

benchmark (double -> string, integer -> string)

License

This project is distributed under the MIT license.