Solved

The fastest / most efficient string to integer conversion.

Posted on 2008-10-12
7
878 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
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
the fastest would be to convert all strings to the integer 0
0
 

Author Comment

by:nanderEire
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:nanderEire
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 103
only14 challenge 19 63
seriesUp challenge 7 106
array6 challenfge 6 62
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now