-
-
Save RainerW/3781245 to your computer and use it in GitHub Desktop.
Compate Apache Common Lang StringUtils split performance with Google Guava Splitter split method.
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
import com.google.common.base.Splitter; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.apache.commons.lang.math.RandomUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.springframework.util.StopWatch; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.regex.Pattern; | |
import static org.junit.Assert.*; | |
public class StringUtilsTest | |
{ | |
// ++++++++++ 'Settings' ++++++++++ | |
// how many different Strings | |
static int testDataCount = 5000; | |
// how many "parts" in one string | |
static int testDataBlockCount = 100; | |
// the separator | |
static String separator = "1234"; | |
// fixed size of one part | |
static int fixedSize = 23; | |
// or maximum random size of a part | |
static Integer randomMaxSize = null; | |
// ++++++++++ | |
static List<String> testData = new LinkedList<String>(); | |
static List<Integer> testDataSize = new LinkedList<Integer>(); | |
static StopWatch stopWatch = new StopWatch(); | |
@BeforeClass | |
public static void generateTestData() { | |
for (int i = 0; i < testDataCount; i++) { | |
String data = ""; | |
for (int j = 0; j < testDataBlockCount; j++) { | |
int size = fixedSize; | |
if (randomMaxSize != null) { | |
size = (RandomUtils.nextInt() % randomMaxSize) + 1; | |
} | |
data += RandomStringUtils.random(size, "ABCDEFGHIJKLMNOPQRSTUVXYZ"); | |
data += separator; | |
} | |
data += RandomStringUtils.random(23, "ABCDEFGHIJKLMNOPQRSTUVXYZ"); | |
testData.add(data); | |
testDataSize.add(data.split(separator).length); | |
} | |
} | |
@AfterClass | |
public static void printTestSummery() { | |
System.out.println(stopWatch.prettyPrint()); | |
} | |
@Test | |
public void javaSplit() throws Exception { | |
Pattern p = Pattern.compile(separator); | |
stopWatch.start("javaSplit"); | |
Iterator<Integer> ref = testDataSize.iterator(); | |
for (String data : testData) { | |
String[] elements = p.split(data); | |
assertEquals((int) ref.next(), elements.length); | |
for (String element : elements) { | |
assertTrue(element.length() < testDataBlockCount); | |
} | |
} | |
stopWatch.stop(); | |
} | |
@Test | |
public void apacheCommonLangSplit() throws Exception { | |
stopWatch.start("Apache Common Lang Split"); | |
for (String data : testData) { | |
String[] elements = StringUtils.split(data, separator); | |
for (String element : elements) { | |
assertTrue(element.length() < testDataBlockCount); | |
} | |
} | |
stopWatch.stop(); | |
} | |
@Test | |
public void guavaSplitterSplit() throws Exception { | |
stopWatch.start("Google Guava Splitter"); | |
Splitter spiltter = Splitter.onPattern(separator); | |
Iterator<Integer> ref = testDataSize.iterator(); | |
for (String data : testData) { | |
Iterable<String> elements = spiltter.split(data); | |
int count = 0; | |
for (String element : elements) { | |
count++; | |
assertTrue(element.length() < testDataBlockCount); | |
} | |
assertEquals((int) ref.next(), count); | |
} | |
stopWatch.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment