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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With