Last active
February 16, 2017 18:01
-
-
Save maxmalysh/8f656be2843b7c35849a2eae2683df0e to your computer and use it in GitHub Desktop.
Benchmark for http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/
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
/* | |
Output: | |
100000 digits | |
3 ms | regex | |
9 ms | lambda | |
11 ms | StringUtils | |
1000000 digits | |
8 ms | regex | |
9 ms | lambda | |
2 ms | StringUtils | |
10000000 digits | |
128 ms | regex | |
56 ms | lambda | |
17 ms | StringUtils | |
100000000 digits | |
690 ms | regex | |
510 ms | lambda | |
161 ms | StringUtils | |
50M-long random string | |
599 ms | regex | |
248 ms | lambda | |
77 ms | StringUtils | |
*/ | |
import org.apache.commons.lang3.RandomStringUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Main { | |
public interface NumericChecker { | |
boolean isNumeric(String string); | |
} | |
public static class TestCase { | |
public String string; | |
public String name; | |
public TestCase(String string, String name) { | |
this.string = string; | |
this.name = name; | |
} | |
long runWith(NumericChecker checker) { | |
long startTime = System.nanoTime(); | |
checker.isNumeric(string); | |
long stopTime = System.nanoTime(); | |
return (stopTime - startTime) / 1000000; | |
} | |
} | |
public static NumericChecker regexChecker = new NumericChecker() { | |
private String regex = "\\d+"; | |
@Override | |
public boolean isNumeric(String string) { | |
return string.matches(regex); | |
} | |
}; | |
public static NumericChecker lamdaChecker = new NumericChecker() { | |
@Override | |
public boolean isNumeric(String string) { | |
return string.chars().allMatch(x -> Character.isDigit(x)); | |
} | |
}; | |
public static NumericChecker stringUtilsChecker = new NumericChecker() { | |
@Override | |
public boolean isNumeric(String string) { | |
return StringUtils.isNumeric(string); | |
} | |
}; | |
public static void main(String[] args) { | |
int[] size = {100000, 1000000, 10000000, 100000000}; | |
List<TestCase> tests = new ArrayList<>(); | |
// Pure numbers | |
for (int N : size) { | |
String string = RandomStringUtils.randomNumeric(N); | |
tests.add(new TestCase(string, String.format("%s digits", N))); | |
} | |
// Not a number, adding just for the sake of diversity | |
String string = new StringBuilder().append(RandomStringUtils.randomNumeric(50000000)) | |
.append("foobar") | |
.append(RandomStringUtils.randomNumeric(50000000)) | |
.toString(); | |
tests.add(new TestCase(string, "50M-long random string")); | |
// Warm-up JIT compiler | |
for (TestCase test : tests.subList(0, 1)) { | |
test.runWith(regexChecker); | |
test.runWith(lamdaChecker); | |
test.runWith(stringUtilsChecker); | |
} | |
for (TestCase test : tests) { | |
long runTimeRegex = test.runWith(regexChecker); | |
long runTimeLambda = test.runWith(lamdaChecker); | |
long runTimeStringUtils = test.runWith(stringUtilsChecker); | |
System.out.println(test.name); | |
System.out.printf("%6d ms | regex \n", runTimeRegex); | |
System.out.printf("%6d ms | lambda \n", runTimeLambda); | |
System.out.printf("%6d ms | StringUtils \n\n", runTimeStringUtils); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment