在Java中将整数转换为字符串的最快方法 [英] Fastest way of converting integer to string in java

查看:425
本文介绍了在Java中将整数转换为字符串的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我必须将int转换为我选择""+aInteger.toString(a)String时.现在我想知道哪种方法更快,因此我编写了一个简单的基准测试,该函数调用function_1,function_2和function_3 10000000次,并打印处理这些函数所需的时间.功能如下:

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

输出为:

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!

我认为function_2是如此之快,因为它被编译为

I think function_2 is so fast, because it is compiled as

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

为避免这种情况,我改用了功能getOne().但是,这是有趣的部分(对我而言):必须不使用Object的原始toString方法(在这种情况下为Integer.toString(1),因为int是原始的)编译function_3.我的问题是:编译器实际上是如何威胁""+1的,因此它比调用Integer.toString(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在编译时是已知的.这就是为什么在转换为字节码时function_2 "" + 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()的结果在编译时未知,因此串联将在运行时完成.但是,由于串联(+)效率不高,编译器可能会将其更改为基于StringBuilder.append()的实现.

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.

不相信我吗?尝试:javap -c ClassName.class,您将看到类似这样的内容:

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()仅具有一个字符串"1",而function_3具有所有这些方法调用,并在其中包含其他StringBuilder:)

function_2() have only one String "1", while function_3 have all these method calls with additional StringBuilder inside :)

请记住,运行时可能会进行一些优化,但这是基于JVM的,并且取决于配置.

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

这篇关于在Java中将整数转换为字符串的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆