将int转换为字符串最有效的方法是什么?

时间:2021-06-13 16:06:21

Say I have:

我有说:

int someValue = 42;

Now I want to convert that int value to a String. Which way is more efficient?

现在我要把那个int值转换成一个字符串。哪种方式更有效?

// 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?

我只是好奇是否有什么真正的区别或者其中一个比另一个好?

6 个解决方案

#1


68  

tested it for 10m assignments of the number 10

测试它的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

#2


36  

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 StringBuilder and appends the components (in this case, the number and the empty string) to that, and finally converts that to a string.

第三种解决方案是不可能的,因为它隐式地创建了一个StringBuilder,并将组件(在本例中,是数字和空字符串)附加到该字符串中,最后将其转换为字符串。

#3


8  

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中连接速度较慢。

#4


8  

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)

和整数。foo toString(int,int基数)

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,你最好调用这个整数。直接toString(int foo)。对于其他情况,使用整数。foo toString(int,int基数)。

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值转换为字符串,然后与空字符串连接。这显然是最昂贵的案例。

#5


5  

(Opposite of David Hanak.)

(大卫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号的速度增长是最小的,我们都知道,“过早的优化是所有邪恶的根源”。

#6


1  

"" + int is slower as shown above by David Hanak.

“+ int速度较慢,如上面大卫·哈纳克所示。”

String.valueOf() inturn calls Integer.toString(). Hence, using Integer.toString() is better.

String.valueOf()内弯调用Integer.toString()。因此,使用int . tostring()更好。

So, Integer.toString() is the fastest..

所以,Integer.toString()是最快的。

#1


68  

tested it for 10m assignments of the number 10

测试它的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

#2


36  

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 StringBuilder and appends the components (in this case, the number and the empty string) to that, and finally converts that to a string.

第三种解决方案是不可能的,因为它隐式地创建了一个StringBuilder,并将组件(在本例中,是数字和空字符串)附加到该字符串中,最后将其转换为字符串。

#3


8  

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中连接速度较慢。

#4


8  

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)

和整数。foo toString(int,int基数)

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,你最好调用这个整数。直接toString(int foo)。对于其他情况,使用整数。foo toString(int,int基数)。

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值转换为字符串,然后与空字符串连接。这显然是最昂贵的案例。

#5


5  

(Opposite of David Hanak.)

(大卫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号的速度增长是最小的,我们都知道,“过早的优化是所有邪恶的根源”。

#6


1  

"" + int is slower as shown above by David Hanak.

“+ int速度较慢,如上面大卫·哈纳克所示。”

String.valueOf() inturn calls Integer.toString(). Hence, using Integer.toString() is better.

String.valueOf()内弯调用Integer.toString()。因此,使用int . tostring()更好。

So, Integer.toString() is the fastest..

所以,Integer.toString()是最快的。