Why is StringBuilder#append(int) faster in Java 7 than in Java 8?

后端 未结 2 1957
不知归路
不知归路 2020-12-12 12:35

While investigating for a little debate w.r.t. using \"\" + n and Integer.toString(int) to convert an integer primitive to a string I wrote this JMH microbenchm

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 13:09

    I think this has to do with the CompileThreshold flag which controls when the byte code is compiled into machine code by JIT.

    The Oracle JDK has a default count of 10,000 as document at http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html.

    Where OpenJDK I couldn't find a latest document on this flag; but some mail threads suggest a much lower threshold: http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/2010-November/004239.html

    Also, try turn on / off the Oracle JDK flags like -XX:+UseCompressedStrings and -XX:+OptimizeStringConcat. I am not sure if those flags are turned on by default on OpenJDK though. Could someone please suggest.

    One experiement you can do, is to firstly run the program by a lot of times, say, 30,000 loops, do a System.gc() and then try to look at the performance. I believe they would yield the same.

    And I assume your GC setting is the same too. Otherwise you are allocating a lot of objects and the GC might well be the major part of your run time.

提交回复
热议问题