Skip to content

Instantly share code, notes, and snippets.

@metab0t
Created June 8, 2022 17:27
Show Gist options
  • Save metab0t/27565abe12aa18b3ffb945ea98d38dc2 to your computer and use it in GitHub Desktop.
Save metab0t/27565abe12aa18b3ffb945ea98d38dc2 to your computer and use it in GitHub Desktop.
`strtod_l` speed

The following two files represent two ways to convert string to double considering locale.

  • strtod_fast.c uses standard C runtime function
  • strtod_slow.c uses a hand written implementation

I experiment the code on 3 compilers

  1. MinGW-UCRT in msys2
$ gcc -v
Using built-in specs.
COLLECT_GCC=D:\msys64\ucrt64\bin\gcc.exe
COLLECT_LTO_WRAPPER=D:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-12.1.0/configure --prefix=/ucrt64 --with-local-prefix=/ucrt64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/ucrt64/include --libexecdir=/ucrt64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/ucrt64 --with-mpfr=/ucrt64 --with-mpc=/ucrt64 --with-isl=/ucrt64 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --disable-libstdcxx-debug --with-boot-ldflags=-static-libstdc++ --with-stage1-ldflags=-static-libstdc++
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (Rev2, Built by MSYS2 project)

Compiled with

$ gcc -O2 strtod_fast.c -o fast.exe
$ gcc -O2 strtod_slow.c -o slow.exe

Result:

$ ./fast.exe 3.1415926
elapsed time: 0.156250
fv = 3.142, dv=3.142

$ ./slow.exe 3.1415926
elapsed time: 0.562500
fv = 3.142, dv=3.142

$ ./fast.exe 0x1p12
elapsed time: 0.046875
fv = 4096.000, dv=4096.000

$ ./slow.exe 0x1p12
elapsed time: 0.250000
fv = 4096.000, dv=4096.000
  1. MinGW-MSVCRT(MinGW64) in msys2
$ gcc -v
Using built-in specs.
COLLECT_GCC=D:\msys64\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-12.1.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --disable-libstdcxx-debug --with-boot-ldflags=-static-libstdc++ --with-stage1-ldflags=-static-libstdc++
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (Rev2, Built by MSYS2 project)

Compiled with

$ gcc -O2 strtod_fast.c -o fast.exe
$ gcc -O2 strtod_slow.c -o slow.exe

Result:

$ ./fast.exe 3.1415926
elapsed time: 0.359375
fv = 3.142, dv=3.142

$ ./slow.exe 3.1415926
elapsed time: 0.531250
fv = 3.142, dv=3.142

$ ./fast.exe 0x1p12    (MSVCRT cannot parse 0x format floating point number at all!)
elapsed time: 0.062500
fv = 0.000, dv=0.000

$ ./slow.exe 0x1p12
elapsed time: 0.171875
fv = 4096.000, dv=4096.000
  1. Arch Linux on WSL2
❯ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC)

Compiled with

$ gcc -O2 strtod_fast.c -o fast
$ gcc -O2 strtod_slow.c -o slow

Result:

❯ ./fast 3.1415926
elapsed time: 0.111824
fv = 3.142, dv=3.142

❯ ./slow 3.1415926
elapsed time: 0.131362
fv = 3.142, dv=3.142

❯ ./fast 0x1p12
elapsed time: 0.040000
fv = 4096.000, dv=4096.000

❯ ./slow 0x1p12
elapsed time: 0.064249
fv = 4096.000, dv=4096.000
#define _GNU_SOURCE
#include <stdlib.h>
#include <locale.h>
#ifdef _WIN32
#define locale_t _locale_t
#define strtod_l _strtod_l
#define strtof_l (float)_strtod_l
#endif
// Cache locale object
static int c_locale_initialized = 0;
static locale_t c_locale;
locale_t get_c_locale(void)
{
if (!c_locale_initialized)
{
c_locale_initialized = 1;
#ifdef _WIN32
c_locale = _create_locale(LC_ALL, "C");
#else
c_locale = newlocale(LC_ALL_MASK, "C", NULL);
#endif
}
return c_locale;
}
double jl_strtod_c(const char *nptr, char **endptr)
{
return strtod_l(nptr, endptr, get_c_locale());
}
float jl_strtof_c(const char *nptr, char **endptr)
{
return strtof_l(nptr, endptr, get_c_locale());
}
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv)
{
const char *p = argv[1];
char *endptr = NULL;
struct timespec t0, t1;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t0);
const int N = 1000000;
float fv;
double dv;
for (int i = 0; i < N; i++)
{
fv = jl_strtof_c(p, &endptr);
dv = jl_strtod_c(p, &endptr);
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t1);
double diff = (double)(t1.tv_sec - t0.tv_sec) + ((double)(t1.tv_nsec - t0.tv_nsec) / 1000000000L);
printf("elapsed time: %f\n", diff);
printf("fv = %.3f, dv=%.3f\n", fv, dv);
return 0;
}
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#define D_PNAN ((double)+NAN)
#define D_PINF ((double)+INFINITY)
int case_insensitive_match(const char *s, const char *t)
{
while (*t && tolower(*s) == *t)
{
s++;
t++;
}
return *t ? 0 : 1;
}
double parse_inf_or_nan(const char *p, char **endptr)
{
double retval;
const char *s;
int negate = 0;
s = p;
if (*s == '-')
{
negate = 1;
s++;
}
else if (*s == '+')
{
s++;
}
if (case_insensitive_match(s, "inf"))
{
s += 3;
if (case_insensitive_match(s, "inity"))
s += 5;
retval = negate ? -D_PINF : D_PINF;
}
else if (case_insensitive_match(s, "nan"))
{
s += 3;
retval = negate ? -D_PNAN : D_PNAN;
}
else
{
s = p;
retval = -1.0;
}
*endptr = (char *)s;
return retval;
}
// Cache locale object
static int c_locale_initialized = 0;
static struct lconv *c_locale;
struct lconv *get_c_locale(void)
{
if (!c_locale_initialized)
{
c_locale_initialized = 1;
c_locale = localeconv();
}
return c_locale;
}
double jl_strtod_c(const char *nptr, char **endptr)
{
char *fail_pos;
double val;
struct lconv *locale_data;
const char *decimal_point;
size_t decimal_point_len;
const char *p, *decimal_point_pos;
const char *end = NULL; /* Silence gcc */
const char *digits_pos = NULL;
int negate = 0;
fail_pos = NULL;
// locale_data = localeconv();
locale_data = get_c_locale();
decimal_point = locale_data->decimal_point;
decimal_point_len = strlen(decimal_point);
decimal_point_pos = NULL;
p = nptr;
/* parse leading spaces */
while (isspace((unsigned char)*p))
{
p++;
}
/* Parse infinities and nans */
val = parse_inf_or_nan(p, endptr);
if (*endptr != p)
return val;
/* Set errno to zero, so that we can distinguish zero results
and underflows */
errno = 0;
/* We process the optional sign manually, then pass the remainder to
the system strtod. This ensures that the result of an underflow
has the correct sign. */
/* Process leading sign, if present */
if (*p == '-')
{
negate = 1;
p++;
}
else if (*p == '+')
{
p++;
}
/* This code path is used for hex floats */
if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X'))
{
digits_pos = p;
p += 2;
/* Check that what's left begins with a digit or decimal point */
if (!isxdigit(*p) && *p != '.')
goto invalid_string;
if (decimal_point[0] != '.' || decimal_point[1] != 0)
{
/* Look for a '.' in the input; if present, it'll need to be
swapped for the current locale's decimal point before we
call strtod. On the other hand, if we find the current
locale's decimal point then the input is invalid. */
while (isxdigit(*p))
p++;
if (*p == '.')
{
decimal_point_pos = p++;
/* locate end of number */
while (isxdigit(*p))
p++;
if (*p == 'p' || *p == 'P')
p++;
if (*p == '+' || *p == '-')
p++;
while (isdigit(*p))
p++;
end = p;
}
else if (strncmp(p, decimal_point, decimal_point_len) == 0)
goto invalid_string;
/* For the other cases, we need not convert the decimal point */
}
}
else
{
/* Check that what's left begins with a digit or decimal point */
if (!isdigit(*p) && *p != '.')
goto invalid_string;
digits_pos = p;
if (decimal_point[0] != '.' || decimal_point[1] != 0)
{
/* Look for a '.' in the input; if present, it'll need to be
swapped for the current locale's decimal point before we
call strtod. On the other hand, if we find the current
locale's decimal point then the input is invalid. */
while (isdigit(*p))
p++;
if (*p == '.')
{
decimal_point_pos = p++;
/* locate end of number */
while (isdigit(*p))
p++;
if (*p == 'e' || *p == 'E')
p++;
if (*p == '+' || *p == '-')
p++;
while (isdigit(*p))
p++;
end = p;
}
else if (strncmp(p, decimal_point, decimal_point_len) == 0)
goto invalid_string;
/* For the other cases, we need not convert the decimal point */
}
}
if (decimal_point_pos)
{
char *copy, *c;
/* Create a copy of the input, with the '.' converted to the
locale-specific decimal point */
copy = (char *)malloc(end - digits_pos + 1 + decimal_point_len);
if (copy == NULL)
{
*endptr = (char *)nptr;
errno = ENOMEM;
return -1.0;
}
c = copy;
memcpy(c, digits_pos, decimal_point_pos - digits_pos);
c += decimal_point_pos - digits_pos;
memcpy(c, decimal_point, decimal_point_len);
c += decimal_point_len;
memcpy(c, decimal_point_pos + 1,
end - (decimal_point_pos + 1));
c += end - (decimal_point_pos + 1);
*c = 0;
val = strtod(copy, &fail_pos);
if (fail_pos)
{
if (fail_pos > decimal_point_pos)
fail_pos = (char *)digits_pos +
(fail_pos - copy) -
(decimal_point_len - 1);
else
fail_pos = (char *)digits_pos +
(fail_pos - copy);
}
free(copy);
}
else
{
val = strtod(digits_pos, &fail_pos);
}
if (fail_pos == digits_pos)
goto invalid_string;
if (negate && fail_pos != nptr)
val = -val;
*endptr = fail_pos;
return val;
invalid_string:
*endptr = (char *)nptr;
errno = EINVAL;
return -1.0;
}
float jl_strtof_c(const char *nptr, char **endptr)
{
return (float)jl_strtod_c(nptr, endptr);
}
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv)
{
const char *p = argv[1];
char *endptr = NULL;
struct timespec t0, t1;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t0);
const int N = 1000000;
float fv;
double dv;
for (int i = 0; i < N; i++)
{
fv = jl_strtof_c(p, &endptr);
dv = jl_strtod_c(p, &endptr);
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t1);
double diff = (double)(t1.tv_sec - t0.tv_sec) + ((double)(t1.tv_nsec - t0.tv_nsec) / 1000000000L);
printf("elapsed time: %f\n", diff);
printf("fv = %.3f, dv=%.3f\n", fv, dv);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment