Packagestring; Public classStringcompareperformance { Public Static voidMain (string[] args) {/*** Comparison of string performance tests*/Long StartTime=System.currenttimemillis (); for(inti = 0; i < 500000000; i++) {String str1= "Zhu Xiao"; String str2= "Zhu Xiao"; } Long endTime=System.currenttimemillis (); System.out. println ("Use string keyword to create strings time:" + (Endtime-starttime) + "milliseconds"); Long startTime1=System.currenttimemillis (); for(inti = 0; i < 500000000; i++) {String str1=NewString ("Chubby"); String str2=NewString ("Chubby"); } Long endTime1=System.currenttimemillis (); System.out.println ("Create string time using String object:" + (ENDTIME1-startTime1)+ "milliseconds"); }}
Test results
Using string keywords to create strings time: 4 ms Creating a string using a String object time: 30 milliseconds
Java Instance – String performance comparison test