help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

istringstream constructor performance


From: RRick
Subject: istringstream constructor performance
Date: Fri, 20 Nov 2009 00:27:24 -0800 (PST)
User-agent: G2/1.0

I have written some C style routines that convert strings to doubles,
ints, etc. using the C++ string stream objects.  Each of the methods
use a locally constructed istringstream object which uses the >>
operator to make the conversions.  A typical method looks like:

double  ToDouble( const string & str)
{
    std::istringstream iss( str);
    double num;
    iss >> num;
    return num;
}

I have heard that the C routines (sprintf and sscanf) are faster that
the corresponding C++ objects.  I did a couple of test and found that:
*     sprintf and ostreamstring are about the same in performance
(within a couple of percents).
*     istringstream (ISS) takes about 82% longer than the sscanf
times.  I tried a couple of variations where I used the ISS clear and
str methods instead of the constructor, but that didn't change the
performance.  When I made the ISS object static, it took only 12%
longer than as sscanf.   The times for each test for 5 million
conversions were:
      ISS local                                 2.79 sec
      ISS static                                1.78 sec
      sscanf                                     1.52 sec

The following static ISS mods made a big difference in performance and
shortened the run by 35%.
double  ToDouble( const string & str)
{
    static std::istringstream iss;

    iss.clear();  iss.str( str);
    double num;
    iss >> num;
    return num;
}

For some reason, running the default ISS constructor takes up a lot of
time.  Why is this?  I have looked at some of the gnu g++ header
source files, but nothing obvious shows up.  Could this an issue with
the basic_stringbuf or basic_streambuf classes?

Any ideas on how to fix this?  I realize an converter object could be
created with an ISS object, but at this point I prefer the ease of use
of the C style methods.  I'm looking for ways to make the construction
of the ISS object much faster.

Is the solution with a different basic_streambuf class?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]