- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在测试用 C++ 格式化 double
的各种方法,下面是我想出的一些代码:
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <sstream>
#include <iostream>
inline long double currentTime()
{
const auto now = std::chrono::steady_clock::now().time_since_epoch();
return std::chrono::duration<long double>(now).count();
}
int main()
{
std::mt19937 mt(std::random_device{}());
std::normal_distribution<long double> dist(0, 1e280);
static const auto rng=[&](){return dist(mt);};
std::vector<double> numbers;
for(int i=0;i<10000;++i)
numbers.emplace_back(rng());
const int precMax=200;
const int precStep=10;
char buf[10000];
std::cout << "snprintf\n";
for(int precision=10;precision<=precMax;precision+=precStep)
{
const auto t0=currentTime();
for(const auto num : numbers)
std::snprintf(buf, sizeof buf, "%.*e", precision, num);
const auto t1=currentTime();
std::cout << "Precision " << precision << ": " << t1-t0 << " s\n";
}
std::cout << "ostringstream\n";
for(int precision=10;precision<=precMax;precision+=precStep)
{
std::ostringstream ss;
ss.precision(precision);
ss << std::scientific;
const auto t0=currentTime();
for(const auto num : numbers)
{
ss.str("");
ss << num;
}
const auto t1=currentTime();
std::cout << "Precision " << precision << ": " << t1-t0 << " s\n";
}
}
让我奇怪的是,起初,当精度小于 40
时,我或多或少获得了相同的性能。但是,2.1x
的区别在于 snprintf
。查看我在 Core i7-4765T、Linux 32 位、g++ 5.5.0、libc 2.14.1 上的输出,使用 -march=native -O3
编译:
snprintf
Precision 10: 0.0262963 s
Precision 20: 0.035437 s
Precision 30: 0.0468597 s
Precision 40: 0.0584917 s
Precision 50: 0.0699653 s
Precision 60: 0.081446 s
Precision 70: 0.0925062 s
Precision 80: 0.104068 s
Precision 90: 0.115419 s
Precision 100: 0.128886 s
Precision 110: 0.138073 s
Precision 120: 0.149591 s
Precision 130: 0.161005 s
Precision 140: 0.17254 s
Precision 150: 0.184622 s
Precision 160: 0.195268 s
Precision 170: 0.206673 s
Precision 180: 0.218756 s
Precision 190: 0.230428 s
Precision 200: 0.241654 s
ostringstream
Precision 10: 0.0269695 s
Precision 20: 0.0383902 s
Precision 30: 0.0497328 s
Precision 40: 0.12028 s
Precision 50: 0.143746 s
Precision 60: 0.167633 s
Precision 70: 0.190878 s
Precision 80: 0.214735 s
Precision 90: 0.238105 s
Precision 100: 0.261641 s
Precision 110: 0.285149 s
Precision 120: 0.309025 s
Precision 130: 0.332283 s
Precision 140: 0.355797 s
Precision 150: 0.379415 s
Precision 160: 0.403452 s
Precision 170: 0.427337 s
Precision 180: 0.450668 s
Precision 190: 0.474012 s
Precision 200: 0.498061 s
所以我的主要问题是:造成这种双重差异的原因是什么?此外,如何使 ostringstream
的性能更接近 snprintf
?
注意:另一个问题,Why is snprintf faster than ostringstream or is it? ,和我的不一样。首先,那里没有具体的答案,为什么不同精度的单个数字的格式化速度较慢。其次,这个问题问的是“为什么它通常比较慢”,这个问题太宽泛,无法回答我的问题,而这个问题问的是格式化单个 double
数字的一种特定情况。
最佳答案
std::ostringstream
电话 vsnprintf
两次:第一次尝试使用小缓冲区,第二次尝试使用正确大小的缓冲区。参见 locale_facets.tcc
大约第 1011 行(此处 std::__convert_from_v
是 vsnprintf
的代理):
#if _GLIBCXX_USE_C99_STDIO
// Precision is always used except for hexfloat format.
const bool __use_prec =
(__io.flags() & ios_base::floatfield) != ios_base::floatfield;
// First try a buffer perhaps big enough (most probably sufficient
// for non-ios_base::fixed outputs)
int __cs_size = __max_digits * 3;
char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
if (__use_prec)
__len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
__fbuf, __prec, __v);
else
__len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
__fbuf, __v);
// If the buffer was not large enough, try again with the correct size.
if (__len >= __cs_size)
{
__cs_size = __len + 1;
__cs = static_cast<char*>(__builtin_alloca(__cs_size));
if (__use_prec)
__len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
__fbuf, __prec, __v);
else
__len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
__fbuf, __v);
}
这与观察结果完全吻合,即对于小要求的精度性能与 snprintf
相同,而对于更高的精度,它的精度要差 2 倍。
此外,由于使用的缓冲区不依赖于 std::ostringstream
的任何属性缓冲区,仅在 __max_digits
上,定义为 __gnu_cxx::__numeric_traits<_ValueT>::__digits10
, 除了修复 libstdc++
之外似乎没有任何自然的修复方法本身。
我已经 reported它是 libstdc++ 的错误。
关于c++ - 为什么 snprintf 在打印单个数字时始终比 ostringstream 快 2 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002264/
我需要将数据从一些 std::ostringstream 写入另一个 std::ostringstream。当然,我可以使用str()函数 std::ostringstream in; std::os
任何人都可以建议一种方法来拥有一个null std::ostringstream这避免了对传递给它的参数做任何工作 ::not_eof(c); } }; // ... nullbuf
不使用科学计数法将double转换为string,建议的方法是 std::ostringstream strStream; strStream << std::fixed; strStream <<
ostringstream ss; ss #include #include #include #include class stackbuf : public std::strea
在我的 C++ 项目中,我试图这样做: std::ostringstream stream(std::ostringstream::out); 但是我得到一个错误: error C2027: use
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicates: Why copying stringstream is not allowed? how copy fro
我有一个奇怪的问题,即使我向它插入输出,ostringstream 还是空的。这是我的代码: //logger.h #include #include #include using std::s
所以我在夏天写了一个 ArrayList 的实现,我有一个 toString 方法,在该方法中我使用 ostringstream 对象来添加字符串,然后输出它们。 方法如下: template std
我的代码有一些问题,因为它输出了两个不同的结果。 代码: void output(int x){ for( int i = 0; i s(result.c_str(), result.c_str()
我有以下代码: int n=2; ostringstream convert; // stream used for the conversion convert (std::ostringstr
我的程序在创建 ostringstream 类的对象时总是崩溃。我无法理解导致它崩溃的原因? 程序: #include #include #include DDF foo::send(const
我试图想出一种聪明的方法来将各种东西连接到一个函数的单个字符串参数中,而不必显式使用 ostringstream。我想到了: #define OSS(...) \ dynamic_cast(std
最近我在使用 C++ 代码时遇到了一个非常奇怪的问题。我在极简主义的例子中重现了这个案例。我们有一个 Egg 类: class Egg { private: const char* name;
在 C++ 中(在带有 gcc 的 Linux 上)我想将字节数组 ( vector ) 放入 ostringstream 中或 string . 我知道我可以使用 sprintf但它似乎不是使用 c
我一直在处理其他人的代码,并注意到在 ostringsteam 的所有使用中,他们习惯于显式附加 std::ends。 这是我从来没有做过的事情,也从来没有遇到过问题。 它似乎没有,但是 std::e
我使用 std::ostringstream用于格式化字符串,它继承了它的 string {ostringstream stream; stream string {
在 MSVC 2005 上,我有以下代码。 std::ostringstream stream("initial string "); stream << 5; std::cout << stream
我正在尝试 this answer 中提供的优雅解决方案但无论我尝试什么,我都无法通过此行的错误 Implicit instantiation of undefined template: std::
我想清除并重用一个 ostringstream(和底层缓冲区),这样我的应用就不必进行那么多分配。如何将对象重置为其初始状态? 最佳答案 我过去使用过 clear 和 str 的序列: // clea
在以下情况下获得不同的输出 std::string temp, temp1 = "foo", temp2 = "bar"; std::vector test; std::ostring
我是一名优秀的程序员,十分优秀!