1

Which one of the following ways is faster? and what is the difference(s)?

int i=0x10;
String s=i+"";
String s1=Integer.toString(i);
String s2=String.valueOf(i);

I don't know if there is another way or not.
Thanks in advance.

  • 8
    Have you tried doing a benchmark yourself? – Joost Mar 13 '14 at 13:09
  • They are the same, just pick one and don't over think about that too much.. – Maroun Mar 13 '14 at 13:10
  • 4
    That sounds like "waste of time" for "micro performance optimization"... – WarrenFaith Mar 13 '14 at 13:11
  • I always use the .toString() since it's easier to see what happens. – Mangs Mar 13 '14 at 13:12
  • Just write a small program that calculates the time and convert integers to string 1.000.000 times with two ways. Then you can see which one is faster. – AloneInTheDark Mar 13 '14 at 13:13
  • @Andynedine The link you provided is abosuletely untrustworthy, its testing methodology is way off. You can't just write three lines of Java code and call it a microbenchmark. – Marko Topolnik Mar 13 '14 at 13:16
  • @MarkoTopolnik I just tested myself myself, and I didn't got any differences, while for several tests. each one answered different result. so how can I benchmark it? –  Mar 13 '14 at 13:17
  • How fast do you need it to be? What if it too a micro-seocnd longer but easier to read and understand? – Peter Lawrey Mar 13 '14 at 13:18
  • @PeterLawrey Sir I always use the first one, but I just was wondering which one is faster? maybe in some extreme applications. –  Mar 13 '14 at 13:20
  • @Joost I tested myself, each run, different results. confused, maybe my CPU is trolling me :( –  Mar 13 '14 at 13:22
  • @parsaporahmad In extreme applications e.g. high frequency trading, I don't use a Strings for integers at all, that will be the fastest option. ;) – Peter Lawrey Mar 13 '14 at 13:22
  • @parsaporahmad you are not running the test for long enough, you should ignore the first ten thousand tries and average the nest ten thousand or so. – Peter Lawrey Mar 13 '14 at 13:22
  • @PeterLawrey Thanks dude, many thanks, I have to give a try. –  Mar 13 '14 at 13:27
  • 1
    @parsaporahmad In Java 7, `System.out.println(i);` doesn't create a String, it converts straight from an integer to writing to the stream without creating a String. i.e. most likely you can eliminate the need for a String at all if it is just a toString() of the integer. – Peter Lawrey Mar 13 '14 at 13:33

3 Answers3

4

Which one of the following ways is faster? and what is the difference(s)?

Take a look at the implementation of String.valueOf(int) (in hyperlinked Grepcode format):

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

This should give you a hint—especially if you are familiar with the concept of method call inlining performed routinely by the JIT compiler on static method calls.

On the topic of proper microbenchmarking, you should use jmh to execute this:

@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1)
@Warmup(iterations = 5, time = 200, timeUnit=MILLISECONDS)
@Measurement(iterations = 30, time = 200, timeUnit=MILLISECONDS)
@State(Scope.Thread)
@Threads(1)
@Fork(1)
public class Measure
{
  private int arg;

  @Setup public void setup() {
    arg = (int) System.nanoTime();
  }

  @GenerateMicroBenchmark public String integerToString() {
    return Integer.toString(arg);
  }

  @GenerateMicroBenchmark public String stringValueOf() {
    return String.valueOf(arg);
  }

  @GenerateMicroBenchmark public String plus() {
    return ""+arg;
  }
}

These were my results:

Benchmark                       Mode   Samples         Mean   Mean error    Units
o.s.Measure.integerToString     avgt        30       53.023        8.868    ns/op
o.s.Measure.plus                avgt        30       54.043        6.833    ns/op
o.s.Measure.stringValueOf       avgt        30       52.071        8.217    ns/op
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0
String s1=Integer.toString(i);
String s2=String.valueOf(i);

In the above two Integer.toString() will be fast as String.valueOf(i) will call the Integer.toString() method to convert.Below is the implementation of toString method

public static String valueOf(int i) {
    return Integer.toString(i);
}
Gundamaiah
  • 780
  • 2
  • 6
  • 30
0
String s=i+"";

In this case first i will be converted into string then it will be appended to string, So already one conversion of i to string ,Then again concatenation, So it will be slowest compared to the other two options.

String s2=String.valueOf(i);

This calls Integer.toString(i, 10); so it will be slower than direct call to Integer.toString(i);

Finally, comparatively faster will be Integer.toString(i);

mahesh
  • 1,311
  • 1
  • 13
  • 26