Skip to content

Instantly share code, notes, and snippets.

@hoffrocket
Forked from anonymous/ReplaceTime.java
Created May 8, 2013 17:46
Show Gist options
  • Save hoffrocket/5542168 to your computer and use it in GitHub Desktop.
Save hoffrocket/5542168 to your computer and use it in GitHub Desktop.
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();
}
}
@hoffrocket
Copy link
Author

String.replace took: 222957 µs
StringUtils.replace took: 107535 µs
String.replace took: 70163 µs
StringUtils.replace took: 11121 µs
String.replace took: 70630 µs
StringUtils.replace took: 12234 µs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment