quick-perf / quickperf

QuickPerf is a testing library for Java to quickly evaluate and improve some performance-related properties
https://github.com/quick-perf/doc/wiki/QuickPerf
Apache License 2.0
484 stars 66 forks source link

Change Concatenation to String Builder (or String Buffer in Case of Multi-threading) #74

Closed ubaid4j closed 4 years ago

ubaid4j commented 4 years ago

So I just checked this class org/quickperf/jvm/jmc/value/DisplayJvmProfilingValueVerifier.java and it has a concatenation which is given below:

        String text =
                  LINE
                + " ALLOCATION (estimations)"                      + "   |   " + "GARBAGE COLLECTION           "                              + "|  THROWABLE"  + LINE_SEPARATOR
                + " Total       : " + thirteen.adapt(allocationTotal) + "|   " + twentyNineLength.adapt("Total pause: " + totalGcPause ) + "|  Exception: "  + exceptionsCount +LINE_SEPARATOR
                + " Inside TLAB : " + thirteen.adapt(insideTlabSum)   + "|   " + twentyNineLength.adapt("Longest GC pause: " + gcPause)  + "|  Error: " + errorCount + LINE_SEPARATOR
                + " Outside TLAB: " + thirteen.adapt(outsideTlabSum)  + "|   " + twentyNineLength.adapt("Young: " + youngGcCollection)   + "|  Throwable: " +throwablesCount + LINE_SEPARATOR
                + LINE
                +  twentyEightLength.adapt(" COMPILATION")                    + "|   " + "CODE CACHE" + LINE_SEPARATOR
                +  twentyEightLength.adapt(" Number: " + compilationsCount)   + "|   " +  codeCacheFullCount + LINE_SEPARATOR
                +  twentyEightLength.adapt(" Longest: " + longestCompilation) + "|   " + LINE_SEPARATOR
                + LINE
                + " " + "JVM" + LINE_SEPARATOR
                + " Name: " + jvmName + LINE_SEPARATOR
                + " Version: " + jvmVersion + LINE_SEPARATOR
                + " Arguments: " + jvmArguments + LINE_SEPARATOR
                + LINE
                + " " + "HARDWARE" + LINE_SEPARATOR
                + " Hardware threads: " + minHwThreads + LINE_SEPARATOR
                + " Cores: " + minNumberOfCores + LINE_SEPARATOR
                + " Sockets: " + minNumberOfSockets + LINE_SEPARATOR
                + " CPU: " + LINE_SEPARATOR
                + cpuDescription + LINE_SEPARATOR
                + LINE
                + " OS:" + LINE_SEPARATOR
                + osVersion + LINE_SEPARATOR
                + LINE;

        System.out.println(text);

So, Can I change it to String Builder (or Buffer in case if it is multi-threaded) cause concatenation is not good for performance wise?

loicmathieu commented 4 years ago

@UbaidurRehman1 starting with Java 9, concatenation is on par with StringBuilder in most cases (as it will be translated to StringBuilder usage at runtime). See https://openjdk.java.net/jeps/280

But even without that, it's only important for code that runs multipe times (what is often called code in the hot path). Here is code that will run once in a test to display the result so it not worth it the optimization.

ubaid4j commented 4 years ago

Nice Explanation, Great.