char array to numeric data types - fastest conversion

E

Eddy C

I know i can convert the char array to a string then to the appropiate
numeric data types (float, double etc) however I'm doing this millions
of times and throughput is critical. Any ideas if there is SDK function
or some example code out there that removes the middle step of creating
a string object.

TIA
 
R

Roedy Green

I know i can convert the char array to a string then to the appropiate
numeric data types (float, double etc) however I'm doing this millions
of times and throughput is critical. Any ideas if there is SDK function
or some example code out there that removes the middle step of creating
a string object.

For hints on the cleanest code, see
http://mindprod.com/applets/converter.html

Everything goes through String, but of course you can write your own.

For example, if you have a char array with digits, and there are
digits in every slot guaranteed, and you don't have a lot of lead
zeroes, you could use shortcut code like this:

int charsToInt( char[] c )
{
int result = 0;
for ( int i=c.length-1; i>=0; i-- )
{
result = result * 10 + ( c - '0' );
}
return result;
}

If you have lead spaces/zeros, best to scan first to get them out of
the way, then give the rest to this method. You may have to test the
char and if it is not in range '0' to '9' throw an exception.
If you are going for speed, you cast off anything not strictly needed.
 
M

Mike Schilling

Eddy C said:
I know i can convert the char array to a string then to the appropiate
numeric data types (float, double etc) however I'm doing this millions
of times and throughput is critical. Any ideas if there is SDK function
or some example code out there that removes the middle step of creating
a string object.

You might look at the JDK source for, say, Integer.parseInt() for hints
about how to do this. I suspect it starts by converting the array to a
character string and then parses it.
 
T

Thomas Hawtin

Eddy said:
I know i can convert the char array to a string then to the appropiate
numeric data types (float, double etc) however I'm doing this millions
of times and throughput is critical. Any ideas if there is SDK function
or some example code out there that removes the middle step of creating
a string object.

Parsing floats and doubles is quite complicated and so probably isn't
particularly fast. It's probably worth benchmarking whether the string
creation is actually the slow part.

If the characters are close together in the source, then it might be
fractionally faster to create a String of a section of data and then use
substring on that. substring (usually) shares the backing char array.
You could even shift the source chars so that they are lined up.

As a complete cheat, so long as you don't have a security manager set,
you could use reflection to change the value of a String instance.
However, I wouldn't recommend it.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top