Fastest way to convert integer to string in java

Every time I had to convert int to String , I chose either ""+a or Integer.toString(a) . Now I was wondering which path is faster, so I wrote a simple test that calls functions_1, function_2 and function_3 10,000,000 times and prints how long it takes to process the functions. Here are the functions:

 public static String i=""; public static String j=""; public static String k=""; public static void function_1() { i=Integer.toString(getOne()); } public static void function_2() { j=""+1; } public static void function_3() { j=""+getOne(); } public static int getOne() { return 1; } 

output:

 Benchmarking starting... Executing function_1 10000000 time(s)... Done executing function_1 in 476 ms. Executing function_2 10000000 time(s)... Done executing function_2 in 8 ms. Executing function_3 10000000 time(s)... Done executing function_3 in 634 ms. Benchmarking complete! 

I think function_2 is so fast because it compiled as

 public static void function_2() { j="1"; } 

therefore, to avoid this, I used the getOne() function instead. But here is the interesting part (for me): function_3 should be compiled without using the original toString Object method (in this case, Integer.toString(1) , because int is primitive). My question is: how does the compiler really threaten ""+1 , so is it slower than calling Integer.toString(1) ?

+5
source share
3 answers

"" and 1 known at compile time. That's why in function_2 "" + 1 really replaced with "1" when converting to bytecode.

getOne() result is unknown at compile time, so concatenation will be performed at runtime. BUT, because concatenation (+) is inefficient, it is likely that the compiler will change this to StringBuilder.append() based on the implementation.

Do not believe me? Try: javap -c ClassName.class and you will see something like this:

 public static void function_2(); Code: 0: ldc #39 // String 1 2: putstatic #16 // Field j:Ljava/lang/String; 5: return public static void function_3(); Code: 0: new #42 // class java/lang/StringBuilder 3: dup 4: invokespecial #44 // Method java/lang/StringBuilder."<init>":()V 7: invokestatic #28 // Method getOne:()I 10: invokevirtual #45 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder; 13: invokevirtual #49 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 16: putstatic #16 // Field j:Ljava/lang/String; 19: return 

function_2() have only one string "1", and function_3 has all these method calls with an optional StringBuilder inside :)

Keep in mind that some optimization may occur at runtime, but this JVM behavior is configuration dependent.

+4
source

I tested the following functions on 10,000,000 iterations:

 public static void no_func_maybe_constant() { j= "" + 1; } public static void no_func_no_constant() { j = ""; j = j + 1; } public static void yes_func_maybe_constant() { j = "" + getOne(); } public static void yes_func_no_constant() { j = ""; j = j + getOne(); } 

My results:

 no_func_maybe_constant Took 0.028058674s no_func_no_constant Took 1.449465242s yes_func_maybe_constant Took 1.275561897s yes_func_no_constant Took 1.263362257s 

The difference between not calling the function and calling the function was really insignificant, so it seems that in the case of "" + 1 it really did calculate the compile time constant. Interestingly, without a function, it sometimes took less time ...

+1
source

The difference between 2 and 3 is probably due to the need to call the method to get an integer. When you call a method, it creates a new activation record that complicates the call stack, so more happens here if the JVM JIT cannot insert this static function call by only one return value (almost certainly this does not happen).

0
source

All Articles