views:

568

answers:

7

Say I have:

int someValue = 42;

Now I want to convert that int value to a String. Which way is more efficient?

// One
String stringValue = Integer.toString(someValue);

// Two
String stringValue = String.valueOf(someValue);

// Three
String stringValue = someValue + "";

I am just curious if there is any real difference or one is better than the other?

+4  A: 

Look at the source code of the JRE and you'll probably see the difference. Or none. In fact the Strinv.valueOf(int foo) is implemented as follows:

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

and the Integer.toString(int foo, int radix)

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

Which means that if you use the radix 10, you better call the Integer.toString(int foo) directly. For the other cases use the Integer.toString(int foo, int radix).

The concat solution first transforms the int value into a String and later concatenates with the empty String. This obviously is the most expensive case.

lewap
But if this path is followed frequently, I believe the JVM will be able to optimistically inline it, making the performance equivalent.
Michael Myers
+5  A: 

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 since concatenation is slow in Java.

Björn
A: 

Hi, Since the versions provided by Sun and expert programmers, the optimized carefully and be sure they are very efficient even for Enterprise stuff. If you will you write your own version you are more than welcome to do so- but won't better than this.

Pooria
Euh... Ascalonian doesn't talk about a "own version"?
Nicolas
+16  A: 

tested it for 10m assignments of 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

One seems to win

Edit: JVM is standard '/usr/bin/java' under 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 edit:

Code as requested

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 similarly
run using

javac *.java; time java One; time java Two; time java Three
cobbal
After a bit of a warm up, 1 and 2 should give the same performance. Probably.
Tom Hawtin - tackline
What JVM did test with ?
Mark Robinson
I have to say, that is some dedication to an answer when you write an actual test for it! I appreciate it
Ascalonian
24 whole lines of code written, plus I wanted to know the answer :)
cobbal
Good answer. Just for the sake of completeness, can you post your code?
Bill the Lizard
I would prefer String.valueOf(...) as it's a static method in the String class returning a String, instead of a static method in the Integer class returning an Integer. It looks like the more appropriate way of doing it.
Ravi Wallau
+11  A: 

Even though according to the measurements of cobbal, #1 seems to be the fastest, I'd strongly recommend the usage of String.valueOf(). My reason for that is that this call does not explicitly contain the type of the argument, so if later on you decide to change it from int to double, there is no need to modify this call. The speed gain on #1 compared to #2 is only 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 appends the components (in this case, the number and the empty string) to that, and finally converts that to a string.

David Hanak
A: 

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

String.valueOf() inturn calls Integer.toString(). Hence, using Integer.toString() is better.

So, Integer.toString() is the fastest..

Chandan .
String.valueOf(int) not String.valueOf(Object)!
Tom Hawtin - tackline
That is for Integer datatype...it calls Integer.toString() directly for int.
Chandan .
The question is about int, not Integer.
Tom Hawtin - tackline
Oh come on, I just overlooked that to begin with. my bad. and String.valueOf(int) calls Integer.toString() anyway. The essence of what I tried to say was that a call to String class eventually is routed to Integer class and that was conveyed. A downvote looks ugly for this answer!
Chandan .
+4  A: 

(Opposite of David Hanak.)

Even though according to the measurements of cobbal, #1 seems to be the fastest, I'd strongly recommend the usage of Integer.toString(). My reason for that is that this call explicitly contains the type of the argument, so if later on you decide to change it from int to double, it is clear that this call has changed. You would do the same if it was a binary format, wouldn't you? The speed gain on #1 compared to #2 is only minimal, and as we all know, "premature optimization is the root of all evil".

Tom Hawtin - tackline
I, as a programmer (of a high level programming language), do not care, at this point, about the TYPE of my argument; I simply want its string representation. String.valueOf() expresses that clearly. IMHO
David Hanak
To be pedantic, String.valueOf does not necessarily return the same result or do the same thing as Integer.toString(int) for all variables. It's those sorts of unexpected semantic differences that drive me to be explicit.
Tom Hawtin - tackline
(That is, of course, IMO.)
Tom Hawtin - tackline