如果性能很重要,我应该使用Java的String.format()吗?

问题描述 投票:197回答:13

我们必须一直为日志输出构建字符串等等。在JDK版本中,我们学会了何时使用StringBuffer(许多附加,线程安全)和StringBuilder(许多附加,非线程安全)。

使用String.format()的建议是什么?它是否有效,或者我们是否被迫坚持连接性能很重要的单线?

例如丑陋的旧风格,

String s = "What do you get if you multiply " + varSix + " by " + varNine + "?";

与整洁的新风格(String.format,可能更慢),

String s = String.format("What do you get if you multiply %d by %d?", varSix, varNine);

注意:我的具体用例是我的代码中的数百个“单行”日志字符串。它们不涉及循环,所以StringBuilder太重了。我特别感兴趣的是String.format()

java string performance string-formatting micro-optimization
13个回答
118
投票

我写了一个小类来测试哪个具有更好的性能,并且+领先于格式。尝试5到6倍。试试吧

import java.io.*;
import java.util.Date;

public class StringTest{

    public static void main( String[] args ){
    int i = 0;
    long prev_time = System.currentTimeMillis();
    long time;

    for( i = 0; i< 100000; i++){
        String s = "Blah" + i + "Blah";
    }
    time = System.currentTimeMillis() - prev_time;

    System.out.println("Time after for loop " + time);

    prev_time = System.currentTimeMillis();
    for( i = 0; i<100000; i++){
        String s = String.format("Blah %d Blah", i);
    }
    time = System.currentTimeMillis() - prev_time;
    System.out.println("Time after for loop " + time);

    }
}

对不同的N运行以上显示两者都表现为线性,但String.format慢5-30倍。

原因是在当前实现中,String.format首先使用正则表达式解析输入,然后填充参数。另一方面,与plus的串联由javac(而不是JIT)优化并直接使用StringBuilder.append


2
投票

我刚刚修改了hhafez的测试以包含StringBuilder。使用XP上的jdk 1.6.0_10客户端,StringBuilder比String.format快33倍。使用-server开关可将系数降低至20。

public class StringTest {

   public static void main( String[] args ) {
      test();
      test();
   }

   private static void test() {
      int i = 0;
      long prev_time = System.currentTimeMillis();
      long time;

      for ( i = 0; i < 1000000; i++ ) {
         String s = "Blah" + i + "Blah";
      }
      time = System.currentTimeMillis() - prev_time;

      System.out.println("Time after for loop " + time);

      prev_time = System.currentTimeMillis();
      for ( i = 0; i < 1000000; i++ ) {
         String s = String.format("Blah %d Blah", i);
      }
      time = System.currentTimeMillis() - prev_time;
      System.out.println("Time after for loop " + time);

      prev_time = System.currentTimeMillis();
      for ( i = 0; i < 1000000; i++ ) {
         new StringBuilder("Blah").append(i).append("Blah");
      }
      time = System.currentTimeMillis() - prev_time;
      System.out.println("Time after for loop " + time);
   }
}

虽然这可能听起来很激烈,但我认为它只在极少数情况下是相关的,因为绝对数字非常低:1百万个简单的String.format调用4秒就好了 - 只要我用它们进行日志记录或者喜欢。

更新:正如sjbotha在评论中所指出的,StringBuilder测试无效,因为它缺少最终的.toString()

String.format(.)StringBuilder的正确加速因子在我的机器上是23(使用-server开关的16)。


1
投票

这是hhafez条目的修改版本。它包括字符串构建器选项。

public class BLA
{
public static final String BLAH = "Blah ";
public static final String BLAH2 = " Blah";
public static final String BLAH3 = "Blah %d Blah";


public static void main(String[] args) {
    int i = 0;
    long prev_time = System.currentTimeMillis();
    long time;
    int numLoops = 1000000;

    for( i = 0; i< numLoops; i++){
        String s = BLAH + i + BLAH2;
    }
    time = System.currentTimeMillis() - prev_time;

    System.out.println("Time after for loop " + time);

    prev_time = System.currentTimeMillis();
    for( i = 0; i<numLoops; i++){
        String s = String.format(BLAH3, i);
    }
    time = System.currentTimeMillis() - prev_time;
    System.out.println("Time after for loop " + time);

    prev_time = System.currentTimeMillis();
    for( i = 0; i<numLoops; i++){
        StringBuilder sb = new StringBuilder();
        sb.append(BLAH);
        sb.append(i);
        sb.append(BLAH2);
        String s = sb.toString();
    }
    time = System.currentTimeMillis() - prev_time;
    System.out.println("Time after for loop " + time);

}

}

循环后的时间391循环后的时间4163循环227后的时间


0
投票

答案很大程度上取决于您的特定Java编译器如何优化它生成的字节码。字符串是不可变的,理论上,每个“+”操作都可以创建一个新的字符串。但是,您的编译器几乎肯定会优化构建长字符串的临时步骤。上面的两行代码完全有可能生成完全相同的字节码。

唯一真正知道的方法是在当前环境中迭代地测试代码。编写一个QD应用程序,以迭代方式连接字符串,并查看它们如何相互超时。


0
投票

考虑在串联中使用"hello".concat( "world!" )来处理少量字符串。性能甚至比其他方法更好。

如果您有超过3个字符串,请考虑使用StringBuilder或String,具体取决于您使用的编译器。


235
投票

我拿了hhafez代码并添加了内存测试:

private static void test() {
    Runtime runtime = Runtime.getRuntime();
    long memory;
    ...
    memory = runtime.freeMemory();
    // for loop code
    memory = memory-runtime.freeMemory();

我为每个方法单独运行它,'+'运算符,String.format和StringBuilder(调用toString()),因此使用的内存不会受到其他方法的影响。我添加了更多连接,使字符串为“Blah”+ i +“Blah”+ i +“Blah”+ i +“Blah”。

结果如下(每次平均5次运行): 接近时间(ms)分配的内存(长) '+'运算符747 320,504 16484的String.format 373312 StringBuilder的769 57,344

我们可以看到String'+'和StringBuilder在时间上几乎完全相同,但StringBuilder在内存使用方面更有效。当我们在足够短的时间间隔内有许多日志调用(或任何其他涉及字符串的语句)时,这非常重要,因此垃圾收集器无法清除由“+”运算符产生的许多字符串实例。

并且注意,BTW,不要忘记在构造消息之前检查日志记录级别。

结论:

  1. 我将继续使用StringBuilder。
  2. 我有太多的时间或太少的生命。

24
投票

这里提出的所有基准都有一些flaws,因此结果不可靠。

我很惊讶没有人用JMH进行基准测试,所以我做到了。

结果:

Benchmark             Mode  Cnt     Score     Error  Units
MyBenchmark.testOld  thrpt   20  9645.834 ± 238.165  ops/s  // using +
MyBenchmark.testNew  thrpt   20   429.898 ±  10.551  ops/s  // using String.format

单位是每秒操作,越多越好。 Benchmark source code。使用OpenJDK IcedTea 2.5.4 Java虚拟机。

所以,旧式(使用+)要快得多。


21
投票

JAVAC 1.6会自动编译您丑陋的旧样式:

StringBuilder sb = new StringBuilder("What do you get if you multiply ");
sb.append(varSix);
sb.append(" by ");
sb.append(varNine);
sb.append("?");
String s =  sb.toString();

所以这与使用StringBuilder完全没有区别。

String.format是更重量级的,因为它创建了一个新的Formatter,解析了你的输入格式字符串,创建了一个StringBuilder,将所有内容附加到它并调用toString()。


12
投票

Java的String.format的工作原理如下:

  1. 它解析格式字符串,爆炸成格式块列表
  2. 它迭代格式块,渲染成StringBuilder,它基本上是一个通过复制到新数组来根据需要调整自身大小的数组。这是必要的,因为我们还不知道分配最终String的大小
  3. StringBuilder.toString()将其内部缓冲区复制到新的String中

如果此数据的最终目的地是流(例如,呈现网页或写入文件),则可以将格式块直接组合到流中:

new PrintStream(outputStream, autoFlush, encoding).format("hello {0}", "world");

我推测优化器会优化格式字符串处理。如果是这样,你将获得相同的amortized性能,以手动将String.format展开到StringBuilder中。


8
投票

为了扩展/纠正上面的第一个答案,实际上并不是String.format会提供帮助的翻译。 String.format将帮助您的是当您打印日期/时间(或数字格式等),其中存在本地化(l10n)差异(即,一些国家将打印04Feb2009而其他国家将打印Feb042009)。 通过翻译,您只是谈论将任何可外部化的字符串(如错误消息和什么不是)移动到属性包中,以便您可以使用ResourceBundle和MessageFormat将正确的语言包用于正确的语言。 综上所述,我会说性能方面,String.format与简单连接归结为你喜欢的。如果您更喜欢查看.format对连接的调用,那么请务必使用它。 毕竟,代码的读取次数远远超过它的编写。


6
投票

在您的示例中,性能probalby并没有太大的不同,但还有其他需要考虑的问题:即内存碎片。甚至连接操作也会创建一个新字符串,即使它是临时的(GC需要时间,而且工作更多)。 String.format()只是更具可读性,它涉及更少的碎片。

此外,如果您经常使用特定格式,请不要忘记您可以直接使用Formatter()类(所有String.format()都会实例化一个使用Formatter实例)。

另外,您应该注意的其他事项:小心使用substring()。例如:

String getSmallString() {
  String largeString = // load from file; say 2M in size
  return largeString.substring(100, 300);
}

那个大字符串仍在内存中,因为这只是Java子字符串的工作方式。更好的版本是:

  return new String(largeString.substring(100, 300));

要么

  return String.format("%s", largeString.substring(100, 300));

如果你同时做其他的事情,第二种形式可能更有用。


5
投票

通常你应该使用String.Format,因为它相对较快并且它支持全球化(假设你实际上是在尝试编写用户读取的东西)。如果您尝试翻译一个字符串而不是每个语句3个或更多(特别是对于具有截然不同的语法结构的语言),它还可以更容易全球化。

现在,如果你从未计划翻译任何东西,那么要么依赖Java内置的+运算符转换为StringBuilder。或者明确地使用Java的StringBuilder


3
投票

从Logging的角度来看另一个视角。

我看到很多关于登录这个线程的讨论,所以想到在回答中添加我的经验。可能有人会觉得它很有用。

我想使用格式化程序进行日志记录的动机来自于避免字符串连接。基本上,如果你不打算记录它,你不希望有字符串concat的开销。

除非你想记录,否则你真的不需要连接/格式化。让我们说如果我定义这样的方法

public void logDebug(String... args, Throwable t) {
    if(debugOn) {
       // call concat methods for all args
       //log the final debug message
    }
}

在这种方法中,如果调试消息和debugOn = false,则根本不会调用cancat / formatter

虽然在这里使用StringBuilder而不是formatter仍然会更好。主要动机是避免任何这种情况。

同时我不喜欢为每个日志语句添加“if”块

  • 它会影响可读性
  • 减少单元测试的覆盖范围 - 当您想要确保每条线路都经过测试时,这会让您感到困惑。

因此,我更喜欢使用上述方法创建一个日志记录实用程序类,并在任何地方使用它,而不必担心性能损失和与之相关的任何其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.