[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 892
  • Last Modified:

The fastest / most efficient string to integer conversion.

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
nanderEire
Asked:
nanderEire
  • 4
  • 2
1 Solution
 
ozoCommented:
the fastest would be to convert all strings to the integer 0
0
 
nanderEireAuthor Commented:
More specifically, which method to convert a string to its integer representation, ie "1234" to 1234, operates the fastest?.
0
 
ozoCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nanderEireAuthor Commented:
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
 
ozoCommented:
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
 
ozoCommented:
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
 
itsmeandnobodyelseCommented:
>>>> 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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now