java 将 int 转换为 String 的最有效方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/653990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the most efficient way to convert an int to a String?
提问by Ascalonian
Say I have:
说我有:
int someValue = 42;
Now I want to convert that int value to a String. Which way is more efficient?
现在我想将该 int 值转换为 String。哪种方式更有效率?
// 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?
我只是好奇是否有任何真正的区别或一个比另一个更好?
回答by cobbal
tested it for 10m assignments of the number 10
对其进行了 10m 编号 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
编辑:JVM 是 Mac OS X 10.5 下的标准“/usr/bin/java”
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
案例 2 和案例 3 类似地
使用
javac *.java; time java One; time java Two; time java Three
回答by David Hanak
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".
尽管根据cobbal的测量,#1 似乎是最快的,但我强烈建议使用String.valueOf(). 我这样做的原因是这个调用没有明确包含参数的类型,所以如果以后你决定将它从 int 更改为 double,则不需要修改这个调用。与#2 相比,#1 的速度提升很小,众所周知,“过早的优化是万恶之源”。
The third solution is out of the question, since it implicitly creates a StringBuilderand appends the components (in this case, the number and the empty string) to that, and finally converts that to a string.
第三种解决方案是不可能的,因为它隐式地创建 aStringBuilder并将组件(在这种情况下,数字和空字符串)附加到它,最后将其转换为字符串。
回答by paweloque
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:
查看 JRE 的源代码,您可能会看到不同之处。或者没有。事实上,Strinv.valueOf(int foo) 的实现如下:
public static String valueOf(int i) {
return Integer.toString(i, 10);
}
and the Integer.toString(int foo, int radix)
和 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).
这意味着如果使用基数 10,则最好直接调用 Integer.toString(int foo)。对于其他情况,请使用 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.
concat 解决方案首先将 int 值转换为 String,然后与空 String 连接。这显然是最昂贵的情况。
回答by Bj?rn
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.
前两个示例实际上是相同的,因为 String.valueOf(int) 使用 Integer.toString(int) 方法。第三个是丑陋的,并且可能效率较低,因为 Java 中的连接速度很慢。
回答by Tom Hawtin - tackline
(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".
尽管根据 cobbal 的测量,#1 似乎是最快的,但我强烈建议使用 Integer.toString()。我这样做的原因是此调用明确包含参数的类型,因此如果稍后您决定将其从 int 更改为 double,很明显此调用已更改。如果它是二进制格式,你也会这样做,不是吗?与#2 相比,#1 的速度提升很小,众所周知,“过早的优化是万恶之源”。
回答by Real Red.
"" + intis slower as shown above by David Hanak.
"" + int速度较慢,如 David Hanak 所示。
String.valueOf()inturn calls Integer.toString(). Hence, using Integer.toString()is better.
String.valueOf()反过来调用Integer.toString()。因此,使用Integer.toString()效果更好。
So, Integer.toString()is the fastest..
所以,Integer.toString()是最快的..

