-
-
Save RainerW/3780998 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
package bausteine; | |
import com.google.common.base.Splitter; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.springframework.util.StopWatch; | |
import org.springframework.util.StringUtils; | |
import java.text.NumberFormat; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.regex.Pattern; | |
import static java.lang.Runtime.getRuntime; | |
import static org.junit.Assert.*; | |
public class StringUtilsTest | |
{ | |
static int testDataCount = 5000; | |
static int testDataStringSize = 10000; | |
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 = RandomStringUtils | |
.random(testDataStringSize, "ABCDEFGHIJKLMNOPQRSTUVXYZ,"); | |
// cleanup for StringUtils.split() | |
data=data.replace(",,", ",x,"); | |
if( data.endsWith(",") ) { | |
data = data.substring(0,data.length()-1); | |
} | |
testData.add(data); | |
testDataSize.add(data.split(",").length); | |
} | |
} | |
@AfterClass | |
public static void printTestSummery(){ | |
System.out.println(stopWatch.prettyPrint()); | |
} | |
@Test | |
public void javaSplit() throws Exception { | |
stopWatch.start("javaSplit"); | |
Pattern p = Pattern.compile(","); | |
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() < testDataStringSize); | |
} | |
} | |
stopWatch.stop(); | |
} | |
@Test | |
public void apacheCommonLangSplit() throws Exception { | |
stopWatch.start("Apache Common Lang Split"); | |
Iterator<Integer> ref = testDataSize.iterator(); | |
for(String data: testData){ | |
String[] elements = StringUtils.split(data, ","); | |
int count = 1; | |
while(elements!=null) { | |
String part = elements[0]; | |
assertTrue(part.length() < testDataStringSize); | |
String rest = elements[1]; | |
elements = StringUtils.split(rest, ","); | |
count ++; | |
} | |
assertEquals(data,(int)ref.next(), count); | |
} | |
stopWatch.stop(); | |
} | |
@Test | |
public void guavaSplitterSplit() throws Exception { | |
stopWatch.start("Google Guava Splitter"); | |
Splitter spiltter = Splitter.on(","); | |
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() < testDataStringSize); | |
} | |
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