Created
September 25, 2012 07:16
-
-
Save chbaranowski/3780424 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 static org.junit.Assert.assertTrue; | |
import java.util.LinkedList; | |
import java.util.List; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.apache.commons.lang.StringUtils; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.springframework.util.StopWatch; | |
import com.google.common.base.Splitter; | |
public class StringUtilsTest | |
{ | |
static int testDataCount = 5000; | |
static int testDataStringSize = 10000; | |
static List<String> testData = new LinkedList<String>(); | |
static StopWatch stopWatch = new StopWatch(); | |
@BeforeClass | |
public static void generateTestData() { | |
for(int i=0; i < testDataCount; i++){ | |
String data = RandomStringUtils | |
.random(testDataStringSize, "ABCDEFGHIJKLMNOPQRSTUVXYZ,"); | |
testData.add(data); | |
} | |
} | |
@AfterClass | |
public static void printTestSummery(){ | |
System.out.println(stopWatch.prettyPrint()); | |
} | |
@Test | |
public void apacheCommonLangSplit() throws Exception { | |
stopWatch.start("Apache Common Lang Split"); | |
for(String data: testData){ | |
String[] elements = StringUtils.split(data, ","); | |
for (String element : elements) { | |
assertTrue(element.length() < testDataStringSize); | |
} | |
} | |
stopWatch.stop(); | |
} | |
@Test | |
public void guavaSplitterSplit() throws Exception { | |
stopWatch.start("Google Guava Splitter"); | |
Splitter spiltter = Splitter.on(","); | |
for(String data: testData){ | |
Iterable<String> elements = spiltter.split(data); | |
for (String element : elements) { | |
assertTrue(element.length() < testDataStringSize); | |
} | |
} | |
stopWatch.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment