I'm trying to compare the performance between the matching with precompiled pattern and a normal matching with regex using the following code. I use JUnit for benchmark testing and it looks like the nonPrecompiled code works faster.
Any ideas why? Do I run the tests in the correct way?
Class code:
import java.util.regex.Pattern;
public class StringSplitter {
private Pattern spacesPattern;
private static final String REGEX_STRING = "\\s+";
public StringSplitter() {
this.spacesPattern = Pattern.compile(REGEX_STRING);
}
public String[] nonPrecompiledTest(String bigS) {
return bigS.split(REGEX_STRING);
}
public String[] precompiledTest(String bigS) {
return this.spacesPattern.split(bigS);
}
}
And the tests code:
import org.junit.BeforeClass;
import org.junit.Test;
import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
public class PatternBenchmarkTest extends AbstractBenchmark {
private static String bigS;
private static StringSplitter pt;
@BeforeClass
public static void prepare() {
char[] bigString = new char[10000000];
for (int i = 0; i < bigString.length; i = i + 2) {
bigString[i] = 'a';
bigString[i + 1] = ' ';
}
bigS = new String(bigString);
System.out.println("Created big string");
pt = new StringSplitter();
System.out.println("Created test class");
}
@Test
public void testNonPrecompiled() {
pt.nonPrecompiledTest(bigS);
}
@Test
public void testPrecompiled() {
pt.precompiledTest(bigS);
}
}
And I get the following result:
Created big string
Created test class
PatternBenchmarkTest.testPrecompiled: [measured 10 out of 15 rounds, threads: 1 (sequential)]
round: 0.33 [+- 0.04], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 9, GC.time: 0.76, time.total: 8.83, time.warmup: 5.56, time.bench: 3.27
PatternBenchmarkTest.testNonPrecompiled: [measured 10 out of 15 rounds, threads: 1 (sequential)]
round: 0.28 [+- 0.04], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 5, GC.time: 0.27, time.total: 4.37, time.warmup: 1.56, time.bench: 2.82