Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to convert numeric chars to int

From looking over here and other websites I know there are two common ways to convert a numeric char value like '5' to an int value:

  1. Using Character.getNumericValue()

  2. Subtracting the number with the ASCII value for '0'; i.e. int number = num - '0', where num is a char value.

Which of these two approaches is the fastest and most efficient?

like image 966
Pepe Avatar asked Sep 19 '19 10:09

Pepe


People also ask

How do you change char to int?

In Java, we can convert the Char to Int using different approaches. If we direct assign char variable to int, it will return the ASCII value of a given character. If the char variable contains an int value, we can get the int value by calling Character. getNumericValue(char) method.

Is Atoi fast?

Atoi is the fastest I could come up with. I compiled with msvc 2010 so it might be possible to combine both templates. In msvc 2010, when I combined templates it made the case where you provide a cb argument slower.

What's the fastest way to convert a string to number?

Use the parseInt() function to convert a string to a number, e.g. const num1 = parseInt(str) .


2 Answers

  1. The two versions are not equivalent:

    • The Character.getNumericalValue(...) methods work for a variety of characters that represent digits or numbers, and it will return -1 or -2 in cases where the character doesn't represent a non-negative integer.
    • The num - '0' approach only gives the correct answer for the codepoints that correspond to the ASCII characters '0' through '9'. For all other codepoints or codeunits, it gives a meaningless value.
  2. The num - '0' version will be faster. This is clear from looking at the source code for getNumericalValue(...).

  3. While the difference is significant in relative terms, it is very small in absolute terms.


I concur with the comments that say that this is most likely a premature optimization.

It is also an incorrect optimization in some contexts.


I use it a lot so was wondering if I was using the most efficient one.

This is definitely premature optimization :-)

The number of times you write a particular code sequence is unrelated to performance of the code sequence when is executed. A section of code is only worth optimizing if the time spent executing it makes a significant difference to your entire application.

like image 116
Stephen C Avatar answered Oct 28 '22 07:10

Stephen C


Well, Character.getNumericValue() takes Unicode, radix, case, culture into account:

 '0' ->  0 // Same as number - '0'
 '9' ->  9 // Same as number - '0'
 'A' -> 10 // Hexadecimal 0xA == 10
 'f' -> 15 // Hexadecimal 0xF == 15
 '³' ->  3 // Unicode superscript 3
 '⒇'-> 20 // Unicode enclosed alphanumeric 20 
 '۵' ->  5 // Persian digit 5
 '*' -> -1 // Doesn't have any corresponding integer value
 '⅚' -> -2 // Even if 5/6 fraction Unicode character is a number, it's not integer

while

number - '0'

is just a subtraction of two ints. That's why Character.getNumericValue() is inevitably slower (some nanoseconds, is it worth optimizing?). But, please, note, that in 'A', 'f', '³', '*' etc. cases you are going to have wrong asnwers with number - '0' code.

like image 32
Dmitry Bychenko Avatar answered Oct 28 '22 07:10

Dmitry Bychenko