2

I am wondering whether "all" C++ formatting libraries eventually fall back to a *sprintf function to format numbers.

I am asking this because:

  • Looking at the iostreams library that comes with Visual C++, I can see that numbers input into a stream will eventuall be formatted with sprintf_s.
  • Boost.Format just uses the available iostreams library as far as I can tell.
  • FastFormat eventually uses vsprintf to format a number.

So, are there iostreams implementations that do not use *sprintf and do the formatting themselves? Are there other formatting libraries that do not forward formatting of numbers to *sprintf family of functions?

I would appreciate answers in the form of:

  • No: implementation XY uses ABC to format numbers
  • Yes: all other (e.g. iostreams) implementations I know (X, Y, Z) also forward number formatting to stdio, because ...

Please avoid overly speculative answers.

4

2 回答 2

4

Boost Spirit 不使用 *printf,从代码(real.hppint.hpp)和例如 ints 和 doubles 的基准中可以看出。

基准测试将 Boost Spirit Karma 的生成器与 Boost.Format 与 sprintf 和 std::stringstream 进行对比。只有 gcc 编译器的 sprintf 性能在该基准测试中才能接近。否则,Boost Spirit 无疑是赢家。

在此处输入图像描述

于 2011-10-07T08:46:36.327 回答
1

不,例如{fmt} 库有自己的整数和浮点格式实现,这比sprintf. 以下是一个平台上双字符串格式基准 ( dtoa-benchmark ) 的结果:

基准测试结果

如您所见,{fmt} 比sprintf这里快约 20 倍。

整数格式化也更快,但没有那么显着(在 Linux 和 macOS 上高达 6-7 倍):

在此处输入图像描述

免责声明:我是 {fmt} 的作者。

于 2012-12-21T22:41:29.347 回答