Fastest way of converting integer to string in java

99封情书 提交于 2019-12-23 10:15:09

问题


Everytime I had to convert an intinto a String I picked either ""+aor Integer.toString(a). Now I wondered which way is faster, so I wrote a simple benchmark that calls function_1, function_2 and function_3 10000000 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;
}

the output is:

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 is compiled as

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

so to avoid that, I used the function getOne() instead. But here is the interesting part(for me): function_3 must be compiled without using the original toString method of Object(in this case Integer.toString(1) because int is primitive). My question is: How does the compiler actually threat ""+1 so it is slower then calling Integer.toString(1)?


回答1:


"" and 1 are known at compile time. This is why in function_2 "" + 1 is really replaced by "1" while convertion to bytecode.

getOne() result is unknown at the compilation time so the concatenation will be done in runtime. BUT because concatenation (+) is not efficient it is likely that compiler will change this to StringBuilder.append() based implementation.

Don't 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", while function_3 have all these method calls with additional StringBuilder inside :)

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




回答2:


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 indeed negligible, so it seems in the case of "" + 1 it was indeed doing some compile-time constant calculation. Interesting that without a function it sometimes took less time...




回答3:


The difference between 2 and 3 is likely due to having to call a method to get the integer. When you call a method, it creates a new activation record which complicates the call stack so there is more going on here unless the JVM's JIT is capable of inlining that static function call to just the single return value (almost certainly not happening here).



来源:https://stackoverflow.com/questions/15669067/fastest-way-of-converting-integer-to-string-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!