Created
February 1, 2013 18:03
-
-
Save PASTJL/4692960 to your computer and use it in GitHub Desktop.
Test String.substring with JDK 1.7.0_6+ and JDK 1.6.38
<PathToJdk6/7>java -Xms128M -Xmx128M teststring.Main 100000 1000000
On my desktop: jdk 1.7.0_11 => 33 seconds / 252 KBytes Memory jdk 1.6.0_38 => 25 milliseconds / 782 KBytes Memory 1000 times faster ! ( Ok! for this stupid test ;-) )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package teststring; | |
import java.util.Random; | |
public class Main { | |
private static long now() { | |
return System.currentTimeMillis(); | |
} | |
/** | |
* @param args | |
*/ | |
static final String[] letters = { "a", "b", "c", "d", "e", "f" }; | |
public static final long usedMemoryInKo() { | |
Runtime rt = java.lang.Runtime.getRuntime(); | |
return (rt.totalMemory() - rt.freeMemory()) / 1024; | |
} | |
public static final String makeLongString(int len) { | |
StringBuilder strBuff = new StringBuilder(); | |
Random rdInt = new Random(); | |
int lenTab = letters.length; | |
for (int i = 0; i < len; i++) { | |
strBuff.append(letters[Math.abs(rdInt.nextInt(lenTab))]); | |
} | |
return strBuff.toString(); | |
} | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
int longString = Integer.parseInt(args[0]); | |
; | |
int nbBoucles = Integer.parseInt(args[1]); | |
String strToParse = makeLongString(longString); | |
long debSubstring1Index = now(); | |
for (int i = 0; i < nbBoucles; i++) { | |
strToParse.substring(i % longString); | |
} | |
long finSubstring1Index = now(); | |
long debSubstring2Index = now(); | |
for (int i = 0; i < nbBoucles; i++) { | |
strToParse.substring(i % longString, longString); | |
} | |
System.out.println("test String.substring(beginIndex) with java " | |
+ System.getProperty("java.version") + " takes " | |
+ (finSubstring1Index - debSubstring1Index) + " ms"); | |
System.out | |
.println("test String.substring(beginIndex,endIndex) with java " | |
+ System.getProperty("java.version") | |
+ " takes " | |
+ (now() - debSubstring2Index) + " ms"); | |
System.gc(); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println("Used memory => " + usedMemoryInKo() + "Ko"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment