Solved

The fastest / most efficient string to integer conversion.

Posted on 2008-10-12
7
887 Views
Last Modified: 2012-06-27
There are several ways to convert strings to integers in C++.

Can you please tell me which of them is the most efficient / fastest option and why?
0
Comment
Question by:nanderEire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22699238
the fastest would be to convert all strings to the integer 0
0
 

Author Comment

by:nanderEire
ID: 22699274
More specifically, which method to convert a string to its integer representation, ie "1234" to 1234, operates the fastest?.
0
 
LVL 84

Expert Comment

by:ozo
ID: 22699368
Some processors have a single machine instruction that does it.
On those processors, using that instruction may be fastest.
If there is enough memory for a table of 2^16 or 2^32 ints, processing the string 2 or 4 bytes at a time may be fastest,
but if the string is the wrong length, that method may try to read beyond the end of the string, which could result in undefined behavior.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:nanderEire
ID: 22700766
Thank you for your assistance. I would like to explore the pre-existing functionality before writing my own.

Of the methods which are available to me, sscanf / atoi / strtod / stringstreams etc. which of those performs its task the fastest? What are the pros and cons of each?

 
0
 
LVL 84

Expert Comment

by:ozo
ID: 22700811
well, since atoi does nothing other than convert a string to an integer, it has the potential to be implemented faster than the other functions that do that as well as other things.
0
 
LVL 84

Expert Comment

by:ozo
ID: 22700841
On the other hand, it could also be implemented as a special case of one of the other functions, in which case just invoking the special case yourself might involve one less subroutine call than calling it, then letting it call the other function.
And if you want to do any of the other things that the other functions do in addition to just getting an integer, then you might prefer to use the more fitting function.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 22704609
>>>> since atoi does nothing other than convert a string to an integer
That is true but atoi stops converting at the first non-digit without error. In case the string might contain nothing or a non-integer you rarely can use atoi.

If you were sure the string is a number (not negative), you might try the following loop

   int i = 0;
   char* psz = strnum;
   while (*psz != '\0') i = i*10 + *psz++;

As it doesn't have a call and no checks beside of checking for zero-termination, it should be even faster than atoi.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question