I am trying some performance benchmark regarding String Pool. However, the outcome is not expected.
I made 3 static methods
- perform0() method ... creates a new object every time
- perform1() method ... String literal "Test"
- perform2() method ... String constant expression "Te"+"st"
My expectation was (1. fastest -> 3. slowest)
- "Test" because of string pooling
- "Te"+"st" because of string pooling but bit slower than 1 because of + operator
- new String(..) because of no string pooling.
But the benchmark shows that "Te"+"st" is slighty faster than "Test".
new String(): 141677000 ns 
"Test"      : 1148000 ns 
"Te"+"st"   : 1059000 ns
new String(): 141253000 ns
"Test"      : 1177000 ns
"Te"+"st"   : 1089000 ns
new String(): 142307000 ns
"Test"      : 1878000 ns
"Te"+"st"   : 1082000 ns
new String(): 142127000 ns
"Test"      : 1155000 ns
"Te"+"st"   : 1078000 ns
...
Here's the code:
import java.util.concurrent.TimeUnit;
public class StringPoolPerformance {
    public static long perform0() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = new String("Test");
        }
        return System.nanoTime()-start;
    }
    public static long perform1() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = "Test";
        }
        return System.nanoTime()-start;
    }
    public static long perform2() {
        long start = System.nanoTime();
        for (int i=0; i<1000000; i++) {
            String str = "Te"+"st";
        }
        return System.nanoTime()-start;
    }
    public static void main(String[] args) {
        long time0=0, time1=0, time2=0;
        for (int i=0; i<100; i++) {
            // result
            time0 += perform0();
            time1 += perform1();
            time2 += perform2();
        }
        System.out.println("new String(): " +  time0 + " ns");
        System.out.println("\"Test\"      : " + time1 + " ns");
        System.out.println("\"Te\"+\"st\"   : " + time2 + " ns");
    }
}
Can someone explain why "Te"+"st" performs faster than "Test"? Is JVM doing some optimizations here? Thank you.
 
     
     
     
     
     
    