Alternative to boost :: lexical_cast

I take part in a call, and just in order to reduce to the limit, in one of the places in my program I need to convert the string to an integer. I tried boost :: lexical_cast, but unfortunately it is sooo sloowwww. I believe all the checks that he performs. I need something to do this conversion without any checks (I know there will be real numbers stored as strings). By the way, using stringstream in a naive way:

stringstream interpreter; interpreter << str; interpreter >> number; 

even slower than boost :: lexical_cast.
Is there an alternative ??

+11
c ++
Jun 19 '11 at 17:47
source share
7 answers

You can do this with sscanf , but I suspect it is slower than atoi as it processes locales.

You will definitely be interested in reading this C ++ Convert String to Int Speed, which implements a naive implementation that is faster than atoi .

EDIT: another post comparing various strings with int implementations: C ++ String to Int .

+18
Jun 19 '11 at 17:50
source share
β€” -

I can recommend Boost Spirit (disassemble with Qi):

.

 #include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; const char *demo1 = "1234"; const char *demo2 = "1234,2345,-777,-888"; const char *demo3 = " 1234 , 2345 , -777, -888 "; void do_demo1() { const char *begin = demo1; const char *iter = begin; const char *end = demo1+strlen(demo1); int result; if (qi::parse(iter, end, qi::int_, result)) std::cout << "result = " << result << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; //// to allow for spaces, use phrase_parse instead of parse // if (qi::phrase_parse(begin, end, qi::int_, qi::space, result) //// ... etc } void do_demo2() { const char *begin = demo2; const char *iter = begin; const char *end = demo2+strlen(demo2); std::vector<int> results; if (qi::parse(iter, end, qi::int_ % ',', results)) std::cout << "results = " << results.size() << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; } void do_demo3() { const char *begin = demo3; const char *iter = begin; const char *end = demo3+strlen(demo3); std::vector<int> results; if (qi::phrase_parse(iter, end, qi::int_ % ',', qi::space, results)) std::cout << "results = " << results.size() << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; } int main() { do_demo1(); do_demo2(); do_demo3(); return 0; } 

Other

Be sure to look at the binary (de) serialization IFF, which you can dictate the format of the stream (text). See my recent answer here for a comparison of methods when it is intended for serialization / deserialization:

  • STL (ANSI C ++ 98 Standard Library)
  • Boost Spirit (above)
  • Update sequence

This publication contains benchmarks

+12
Jun 19 '11 at 18:30
source share

In Google Summer of Code, I am working on a new Boost library to solve this problem; boost :: coerce, which can be found here here . The backend is based on boost :: spirit, which provides you with all its advantages (in particular, speed) with a much simpler interface:

 int i = boost::coerce::as<int>("23"); 

or

 std::string s = boost::coerce::as<std::string>(23); 

Please note that it still works, but should be fairly stable in practice. If you have any problems, let me know.

+8
Jun 19 '11 at 18:46
source share

strtol may be better than atoi (specifically wrt error handling) and will be faster than lexical_cast.

+1
Jun 19 '11 at 17:50
source share

The atoi / itoa functions are usually faster, like sscanf ().

All of this is from the c runtime, but they should work well for you.

+1
Jun 19 '11 at 17:50
source share

If you really don't need to do any checks, the fastest way might be to convert the string yourself. I mean something like this:

 int integer_from(string s) { int n = 0; for (string::const_iterator it = s.begin(); it != s.end(); it++) { n = 10*n + (*it) - '0'; } return n; } 
0
Jun 20 2018-11-11T00:
source share

how about using stoi (). I am sure that it should be fast enough to satisfy your needs.

0
Jan 09 '14 at 18:11
source share



All Articles