69

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?

Ascalonian
  • 14,409
  • 18
  • 71
  • 103

7 Answers7

81

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
  • 69,903
  • 20
  • 143
  • 156
  • 1
    After a bit of a warm up, 1 and 2 should give the same performance. Probably. – Tom Hawtin - tackline Mar 17 '09 at 12:41
  • 2
    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 Mar 18 '09 at 04:17
  • 1
    But the source code from String.valueOf(s) is "return Integer.toString(s)" – Gilian Feb 25 '17 at 22:51
  • I would be surprised if the compilers still don't know how to optimize ""+int ... if not, eventually they should recognize that and make it a simple String conversion. At that point, it will be the fastest because you saved a few keystrokes. – Patrick Parker Apr 29 '18 at 19:54
  • 1
    With warmup, on a recent JDK, I get very, very different results: essentially a tie. – tucuxi Oct 15 '21 at 10:39
37

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.

Community
  • 1
  • 1
David Hanak
  • 10,754
  • 3
  • 31
  • 39
  • Benchmarking results disagree with `""+i` being so slow. It appears to get optimized to be as fast as the alternatives. – tucuxi Oct 15 '21 at 10:42
8

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.

paweloque
  • 18,466
  • 26
  • 80
  • 136
8

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
  • 29,019
  • 9
  • 65
  • 81
5

(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
  • 145,806
  • 30
  • 211
  • 305
  • 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 Mar 17 '09 at 12:48
  • 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 Mar 17 '09 at 13:25
  • If the intent or semantics are "I want to take an object of types other than just integer, and get the string form of it", String.valueOf expresses that better. If the intent or semantics are "I have to take an integer, and get the string form of it", Integer.toString expresses that better. – mtraceur Dec 31 '19 at 17:10
3

"" + 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..

Blasanka
  • 21,001
  • 12
  • 102
  • 104
Real Red.
  • 4,991
  • 8
  • 32
  • 44
0

My micro-benchmark results for JDK 11 are very different from those of the currently-accepted answer, showing minimal run-time difference, and no memory use/allocation difference at all:

 t (ms)   method

 12,30    "" + j
 12,57    Integer.toString(j)
 12.58    String.valueOf(j)

I actually wrote this microbenchmark to scratch an itch for a different question; a commenter suggested that I also post here. Code to reproduce (also from that post) follows:

package org.example;

import com.google.caliper.BeforeExperiment;
import com.google.caliper.Benchmark;
import com.google.caliper.Param;
import com.google.caliper.runner.CaliperMain;

public class Main {

    @Param({"10", "100", "1000"})
    int size;

    private String[] output;

    @BeforeExperiment
    void setUp() {
        output = new String[size];
    }

    @Benchmark void quote(int reps) {
        for (int i = 0; i < reps; i++) {
            for (int j = 0; j < size; j++) {
                output[j] = "" + j;
            }
        }
    }

    @Benchmark void toString(int reps) {
        for (int i = 0; i < reps; i++) {
            for (int j = 0; j < size; j++) {
                output[j] = Integer.toString(j);
            }
        }
    }

    @Benchmark void valueOf(int reps) {
        for (int i = 0; i < reps; i++) {
            for (int j = 0; j < size; j++) {
                output[j] = String.valueOf(j);
            }
        }
    }

    /*
    must have com.google.caliper:caliper:1.0-beta-3 in pom.xml; run with:
    mvn clean compile exec:java -Dexec.mainClass="com.google.caliper.runner.CaliperMain" -Dexec.args="org.example.Main"
     */
    public static void main(String[] args) {
        CaliperMain.main(Main.class, args);
    }
}
tucuxi
  • 17,561
  • 2
  • 43
  • 74