Aug. 30th, 2010
curious benchmark
Aug. 30th, 2010 02:24 pmTried to figure out whether
here's what I got:
Oh fcuk! The pattern was wrong, it was successfully failing on the first character!
Here are the right results:
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... )
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... )
visitor pattern, scala sample
Aug. 30th, 2010 08:29 pm
scala> abstract class N[T]
defined class N
scala> case class L[T](v:T) extends N[T]
defined class L
scala> case class B[T](kids:List[N[T]]) extends N[T]
defined class B
scala> val t = B(List(B(List(L("ab"),L("cd"),B(List(L("xy"),L("z"))))),L("middle")))
t: B[java.lang.String] = B(List(B(List(L(ab), L(cd), B(List(L(xy), L(z))))), L(middle)))
scala> def scan[T](tree:N[T]):List[T] = tree match { case L(v) => List(v); case B(kids) => kids flatMap (scan(_)) }
scan: [T](N[T])List[T]
scala> scan(t)
res17: List[java.lang.String] = List(ab, cd, xy, z, middle)
scala>
I used repl, so it's kind of too laconic; in real life you probaly won't call your class N, L and B. But you know what I mean, right?