Tried to figure out whether
String.contains()
is better or worse than
Pattern.compile()
(do this once)
.matcher().matches()
:
here's what I got:
StringContains(ab,1000): 0.4291536470 sec/million
PatternMatches(ab,1000): 0.1891851651 sec/million
StringContains(abcd,1000): 0.4248620093 sec/million
PatternMatches(abcd,1000): 0.2675056776 sec/million
StringContains(abcdefgh,1000): 0.4405976392 sec/million
PatternMatches(abcdefgh,1000): 0.1933575138 sec/million
Oh fcuk! The pattern was wrong, it was successfully failing on the first character!Here are the right results:
StringContains(1,5000): 0.2350807750 sec/million
PatternMatches(1,5000): 74.5895135201 sec/million
StringContains(2,5000): 1.8787401950 sec/million
PatternMatches(2,5000): 154.4377975827 sec/million
StringContains(4,5000): 2.3345991795 sec/million
PatternMatches(4,5000): 169.6984495178 sec/million
StringContains(8,5000): 1.6679779701 sec/million
PatternMatches(8,5000): 105.2310321675 sec/million
StringContains(16,5000): 2.3345991795 sec/million
PatternMatches(16,5000): 172.8726651203 sec/million
StringContains(32,5000): 2.2831006681 sec/million
PatternMatches(32,5000): 165.0592113295 sec/million
StringContains(64,5000): 2.2811933159 sec/million
PatternMatches(64,5000): 162.8616774509 sec/million
StringContains(128,5000): 2.2935889183 sec/million
PatternMatches(128,5000): 168.4775973630 sec/million
StringContains(256,5000): 2.3403212363 sec/million
PatternMatches(256,5000): 170.7972164571 sec/million
StringContains(512,5000): 2.3403212363 sec/million
PatternMatches(512,5000): 171.5297277500 sec/million
The first parameter is the sample length ("a", "ab", "abcd" etc); the second is the length of a randomly-built string that, in half of the cases, contains the sample at a random position.
( Read more... )