性能:Java的String.format [英] Performance: Java's String.format
问题描述
我想知道在Java应用程序中使用 String.format
而不是 StringBuilder是否合适
...所以,我只是写一个简单的测试,如下所示:
I was wondering if is good to use String.format
in Java apps instead of StringBuilder
... so, I just write a simple test, like this:
public static void main(String[] args) {
int i = 0;
Long start = System.currentTimeMillis();
while (i < 10000) {
String s = String.format("test %d", i);
i++;
}
System.out.println(System.currentTimeMillis() - start);
i = 0;
start = System.currentTimeMillis();
while (i < 10000) {
String s = new StringBuilder().append("test ").append(i).toString();
i++;
}
System.out.println(System.currentTimeMillis() - start);
}
结果在哪里:
238
15
所以,如果我的测试有效, StringBuilder
比 String.format
快。好。
现在,我开始思考 String.format
的工作原理。它是一个简单的字符串连接,如test+ i
?
So, if my test is valid, StringBuilder
is faster than String.format
. OK.
Now, I start thinking how String.format
works. Is it a simple String concatenation, like "test " + i
?
<$ c $之间的差异是什么c> StringBuilder 连接和 String.format
?有没有像 String.format
这样的简单方法,并且快速像 StringBuilder
?
What the differences between StringBuilder
concatenation and String.format
? Is there a way simple as String.format
and fast like StringBuilder
?
推荐答案
我写了一个快速 caliper 基准比较 String.format()
与 StringBuilder
, StringBuffer
,普通字符串
+
运算符, String.replace()
和 String.concat()
方法:
I wrote a quick caliper benchmark to compare String.format()
vs. StringBuilder
, StringBuffer
, normal String
+
operator, String.replace()
and String.concat()
methods:
public class StringFormatBenchmark extends SimpleBenchmark {
public void timeStringFormat(int reps) {
while (--reps >= 0) {
String s = String.format("test %d", reps);
}
}
public void timeStringBuilder(int reps) {
while (--reps >= 0) {
String s = new StringBuilder("test ").append(reps).toString();
}
}
public void timeStringBuffer(int reps) {
while (--reps >= 0) {
String s = new StringBuffer("test ").append(reps).toString();
}
}
public void timeStringPlusOperator(int reps) {
while (--reps >= 0) {
String s = "test " + reps;
}
}
public void timeReplace(int reps) {
while (--reps >= 0) {
String s = "test {}".replace("{}", String.valueOf(reps));
}
}
public void timeStringConcat(int reps) {
while (--reps >= 0) {
String s = "test ".concat(String.valueOf(reps));
}
}
public static void main(String[] args) {
new Runner().run(StringFormatBenchmark.class.getName());
}
}
结果如下(Java 1.6。 0_26-b03,Ubuntu,32位):
The results follow (Java 1.6.0_26-b03, Ubuntu, 32 bits):
显然 String.format()
要慢得多(按一个数量级)。此外 StringBuffer
比 StringBuilder
慢得多(正如我们所教导的那样)。最后 StringBuilder
和字符串
+
运算符几乎相同,因为它们编译为非常相似的字节码。 String.concat()
有点慢。
Clearly String.format()
is much slower (by an order of magnitude). Also StringBuffer
is considerably slower than StringBuilder
(as we were taught). Finally StringBuilder
and String
+
operator are almost identical since they compile to very similar bytecode. String.concat()
is a bit slower.
也不要使用字符串.replace()
如果简单连接就足够了。
Also don't use String.replace()
if simple concatenation is sufficient.
这篇关于性能:Java的String.format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!