Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to boost::lexical_cast

Tags:

c++

I'm taking a part in a challenge, and just to cut to the point, in one of places in my program I need to convert string to an integer. I've tried boost::lexical_cast but unfortunatelly it is sooo sloowwww. I suppose because all of the checks it performs. What I need is something that would perform this conversion without any checks (I know that there will be valid numbers stored as strings). By the way using stringstream in the naive way:

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

is even slower than boost::lexical_cast.
Is atoi the only alternative?

like image 274
smallB Avatar asked Jun 19 '11 17:06

smallB


1 Answers

You could do it using sscanf but I suspect it's slower than atoi as it handles locales.

You'll definitely be interested in reading this C++ Convert String to Int Speed benchmark that features a naive implementation that is faster than atoi.

EDIT: Another post comparing different string to int implementations: C++ String to Int.

like image 61
Gregory Pakosz Avatar answered Sep 19 '22 17:09

Gregory Pakosz