将长日志分为两个的性能问题 [英] Performance issue with Splitting long log line into two

查看:131
本文介绍了将长日志分为两个的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将一长串代码分成两行以便于阅读时,它会在行之间引入一个加号(如果在文本之间拆分)。
例如,在文本中间分割一条记录一些文本的长行。

When we split a long line of code into two for readability, it introduces a plus sign between the lines (if split between the text). For example splitting a long line in middle of text which is logging some text.

所以,如果它带来了额外的字符串连接,应该避免这种情况吗?或者更好的可读性权衡更重?

So, this should be avoided given additional string concatenation it brings into picture ? Or trade off of better readability weighs higher ?

推荐答案

所有常见的Java编译器都太成熟了,无法像连接字符串那样做哑巴运行时的文字。让我们检查。鉴于此代码:

All the usual Java compilers are way too mature to do something dumb like concatenate string literals at run time. Let's check. Given this code:

public class CatStrings {
  public static void main(String [] args) {
    String a = "This is a long long long string broken up "
        + "into parts to see if the compiler "
        + "optimizes the concatenation.";
    System.out.println(a);
  }
}

我的Java 8编译器 - Oracle标准 - 做得对 javap 输出显示的东西:

My Java 8 compiler - Oracle standard - does the right thing as shown by javap output:

  stack=2, locals=2, args_size=1
     0: ldc           #2                  // String This is a long long long string broken up into parts to see if the compiler optimizes the concatenation.
     2: astore_1
     3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
     6: aload_1
     7: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    10: return

这篇关于将长日志分为两个的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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