Aug. 30th, 2010

juan_gandhi: (Default)
org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
juan_gandhi: (Default)
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... )
juan_gandhi: (Default)

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?

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

June 2025

S M T W T F S
1 2345 6 7
8 9 10 11 121314
15161718 1920 21
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 28th, 2025 11:13 am
Powered by Dreamwidth Studios