c ++ stringstream太慢了,怎么加快?

hjb*_*reg 18 c++ performance parsing

可能重复:
在C++中从文本文件中读取数值的最快方法(在这种情况下为double)

#include <ctime>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
#include <limits>

using namespace std;

static const double NAN_D = numeric_limits<double>::quiet_NaN();

void die(const char *msg, const char *info)
{
    cerr << "** error: " << msg << " \"" << info << '\"';
    exit(1);
}

double str2dou1(const string &str)
{
    if (str.empty() || str[0]=='?') return NAN_D;
    const char *c_str = str.c_str();
    char *err;
    double x = strtod(c_str, &err);
    if (*err != 0) die("unrecognized numeric data", c_str);
    return x;
}

static istringstream string_to_type_stream;

double str2dou2(const string &str)
{
    if (str.empty() || str[0]=='?') return NAN_D;
    string_to_type_stream.clear();
    string_to_type_stream.str(str);
    double x = 0.0;
    if ((string_to_type_stream >> x).fail())
        die("unrecognized numeric data", str.c_str());
    return x;
}

int main()
{
    string str("12345.6789");

    clock_t tStart, tEnd;

    cout << "strtod: ";
    tStart=clock();

    for (int i=0; i<1000000; ++i)
        double x = str2dou1(str);

    tEnd=clock();
    cout << tEnd-tStart << endl;

    cout << "sstream: ";
    tStart=clock();

    for (int i=0; i<1000000; ++i)
        double x = str2dou2(str);

    tEnd=clock();
    cout << tEnd-tStart << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

strtod:405
sstream:1389

更新:删除undersocres,env:win7 + vc10

650*_*502 9

C/C++文本到数字格式很慢.流速非常慢,但即使是C数字解析也很慢,因为很难将其校正到最后一个精度位.

在一个生产应用程序中,读取速度很重要,并且已知数据最多有三个十进制数字且没有科学记数法,我通过手工编写浮点解析函数只得到符号,整数部分和任意数量的小数来获得了巨大的改进(通过"浩瀚",我的意思是相比,快10倍strtod.

如果你不需要exponent并且这个函数的精度足够,那么这就是我所写的解析器的代码.在我的电脑上,它现在比strtod快6.8倍,比sstream快22.6倍.

double parseFloat(const std::string& input)
{
    const char *p = input.c_str();
    if (!*p || *p == '?')
        return NAN_D;
    int s = 1;
    while (*p == ' ') p++;

    if (*p == '-') {
        s = -1; p++;
    }

    double acc = 0;
    while (*p >= '0' && *p <= '9')
        acc = acc * 10 + *p++ - '0';

    if (*p == '.') {
        double k = 0.1;
        p++;
        while (*p >= '0' && *p <= '9') {
            acc += (*p++ - '0') * k;
            k *= 0.1;
        }
    }
    if (*p) die("Invalid numeric format");
    return s * acc;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

字符串流慢。相当非常缓慢。如果您正在编写任何对大型数据集起作用的关键性能(例如在游戏期间级别更改后加载资产),请不要使用字符串流。我建议使用老式的 c 库解析函数来提高性能,尽管我不能说它们与 boost Spirit 之类的东西相比如何。

但是,与 c 库函数相比,字符串流非常优雅、可读和可靠,因此如果您所做的不是性能关键,我建议坚持使用流。


Boa*_*niv 5

一般来说,如果您需要速度,请考虑这个库:

http://www.fastformat.org/

(不过,我不确定它是否包含将字符串或流转换为其他类型的函数,因此它可能无法回答您当前的示例)。

作为记录,请注意您在这里将苹果与橙子进行比较。strtod()是一个具有单一用途(将字符串转换为双精度)的简单函数,而 stringstream 是一种复杂得多的格式化机制,它远未针对该特定用途进行优化。更公平的比较是将 stringstream 与 sprintf/sscanf 函数行进行比较,后者会比strtod()stringstream 慢,但仍然快。我不太确定是什么让 stringstream 的设计比 sprintf/sscanf 慢,但似乎就是这样。

  • @hjbreg,因为它必须支持语言环境。 (6认同)

Let*_*_Be 2

你考虑过使用lexical_castboost吗?

http://www.boost.org/doc/libs/1_46_1/libs/conversion/lexical_cast.htm

编辑:顺便说一句,clear()应该是多余的。

  • @Let_Me_Be: lexical_cast 是使用 stringstream 实现的;-) (3认同)