Last active
February 6, 2016 16:43
-
-
Save maxmalysh/a991bbe4a923539f19fb to your computer and use it in GitHub Desktop.
Benchmark for http://stackoverflow.com/questions/15111420/how-to-check-if-a-string-contains-only-digits-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
public class Main { | |
public interface NumericChecker { | |
boolean isNumeric(String string); | |
} | |
public static class TestCase { | |
public String string; | |
public TestCase(String string) { | |
this.string = string; | |
} | |
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 parallelChecker = new NumericChecker() { | |
@Override | |
public boolean isNumeric(String string) { | |
return string.chars().parallel().allMatch(x -> Character.isDigit(x)); | |
} | |
}; | |
public static void main(String[] args) { | |
int[] size = {500000, 5000000, 50000000, 100000000}; | |
List<TestCase> tests = new ArrayList<>(); | |
// Pure numbers | |
for (int N : size) { | |
String string = RandomStringUtils.randomNumeric(N); | |
tests.add(new TestCase(string)); | |
} | |
// 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)); | |
// Warm-up JIT compiler | |
for (TestCase test : tests.subList(0, 1)) { | |
test.runWith(regexChecker); | |
test.runWith(lamdaChecker); | |
test.runWith(parallelChecker); | |
} | |
for (TestCase test : tests) { | |
long runTimeRegex = test.runWith(regexChecker); | |
long runTimeLambda = test.runWith(lamdaChecker); | |
long runTimeParallel = test.runWith(parallelChecker); | |
System.out.printf("%6d ms\n", runTimeRegex); | |
System.out.printf("%6d ms\n", runTimeLambda); | |
System.out.printf("%6d ms\n\n", runTimeParallel); | |
} | |
} | |
} | |
/* | |
Output: | |
4 ms | |
2 ms | |
10 ms | |
33 ms | |
21 ms | |
18 ms | |
285 ms | |
224 ms | |
210 ms | |
571 ms | |
465 ms | |
360 ms | |
537 ms | |
232 ms | |
227 ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment