Ascalonian
Ascalonian

Reputation: 15203

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

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?

Upvotes: 70

Views: 26043

Answers (7)

tucuxi
tucuxi

Reputation: 17955

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);
    }
}

Upvotes: 0

Real Red.
Real Red.

Reputation: 5049

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

Upvotes: 3

cobbal
cobbal

Reputation: 70795

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

Upvotes: 82

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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

Upvotes: 5

David Hanak
David Hanak

Reputation: 10984

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.

Upvotes: 37

paweloque
paweloque

Reputation: 18904

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.

Upvotes: 8

Bj&#246;rn
Bj&#246;rn

Reputation: 29411

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.

Upvotes: 8

Related Questions