Skip to content

Instantly share code, notes, and snippets.

Created May 8, 2013 17:45
Show Gist options
  • Save anonymous/5542156 to your computer and use it in GitHub Desktop.
Save anonymous/5542156 to your computer and use it in GitHub Desktop.
micro benchmark of String.replace vs StringUtils.replace
import org.apache.commons.lang.StringUtils;
public class ReplaceTime
{
static final int LOOPS = 100000;
static void timeReplace() {
long start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
"foo;bar;car".replace(";", "%3B");
}
System.out.println("String.replace took: " + (System.nanoTime() - start)/1000 + " µs");
}
static void timeStringUtilsReplace() {
long start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
StringUtils.replace("foo;bar;car", ";", "%3B");
}
System.out.println("StringUtils.replace took: " + (System.nanoTime() - start)/1000 + " µs");
}
public static void main( String[] args )
{
timeReplace();
timeStringUtilsReplace();
timeReplace();
timeStringUtilsReplace();
timeReplace();
timeStringUtilsReplace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment