What is the most efficient way to convert int to String?

Let's say I have:

int someValue = 42; 

Now I want to convert this int value to String. Which method is more effective?

 // One String stringValue = Integer.toString(someValue); // Two String stringValue = String.valueOf(someValue); // Three String stringValue = someValue + ""; 

I'm just wondering if there is any real difference or if one is better than the other?

+51
java string
Mar 17 '09 at 12:23
source share
6 answers

checked it on 10 m of assigning the number 10

 One: real 0m5.610s user 0m5.098s sys 0m0.220s Two: real 0m6.216s user 0m5.700s sys 0m0.213s Three: real 0m12.986s user 0m11.767s sys 0m0.489s 

Seems to win

Edit: JVM is the standard "/ usr / bin / java" on Mac OS X 10.5

 java version "1.5.0_16"
 Java (TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
 Java HotSpot (TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

More rights:

Code on request

 public class One { public static void main(String[] args) { int someValue = 10; for (int i = 0; i < 10000000; i++) { String stringValue = Integer.toString(someValue); } } } 

case 2 and 3 are similar
use

 javac *.java; time java One; time java Two; time java Three 
+64
Mar 17 '09 at 12:32
source share

Despite the fact that according to cobbal measurements , # 1 seems to be the fastest, I highly recommend using String.valueOf() . My reason is that this call does not explicitly contain an argument type, so if you later decide to change it from int to double, there is no need to change this call. The increase in speed by # 1 compared to No. 2 is minimal, and, as we all know, "premature optimization is the root of all evil."

The third solution is out of the question since it implicitly creates a StringBuilder and adds components to it (in this case, a number and an empty string) and finally converts them to a string.

+32
Mar 17 '09 at 12:38
source share

The first two examples are actually identical, since String.valueOf (int) uses the Integer.toString (int) method. The third is ugly and probably less efficient, as Java concatenation is slow.

+8
Mar 17 '09 at 12:29
source share

Take a look at the source code for the JRE and you will probably see the difference. Or not. In fact, Strinv.valueOf (int foo) is implemented as follows:

 public static String valueOf(int i) { return Integer.toString(i, 10); } 

and Integer.toString (int foo, int radix)

 public static String toString(int i, int radix) { ... if (radix == 10) { return toString(i); } ... } 

This means that if you are using radix 10, you are best off calling Integer.toString (int foo). For other cases, use Integer.toString (int foo, int radix).

The concatenated solution first converts the int value to a string and then concatenates with the empty string. This is obviously the most expensive case.

+7
Mar 17 '09 at 12:28
source share

(Opposite David Hanak.)

Despite the fact that according to cobbal measurements, # 1 seems to be the fastest, I highly recommend using Integer.toString (). My reason is that this call explicitly contains the type of the argument, so if you later decide to change it from int to double, it becomes clear that this call has changed. You would do the same if it were a binary format, right? The increase in speed by # 1 compared to No. 2 is minimal, and, as we all know, "premature optimization is the root of all evil."

+5
Mar 17 '09 at 12:43
source share

"" + int slower as shown above by David Hanak.

String.valueOf() calls inturn Integer.toString() . Therefore, it is better to use Integer.toString() .

So Integer.toString() is the fastest ..

+1
Mar 17 '09 at 12:43
source share



All Articles