From: Brett Simmers Date: 2007-08-03T01:30:58+09:00 Subject: Re: sytnax --------------030703000704080507030407 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Phlip wrote: > Shai Rosenfeld wrote: > > >>> var = { 1 => 'one', 2 => 'two', 3 => 'three' }.fetch(number, 'else') >>> >> nice! :) >> >> any lead to which one is faster? >> > > D'OH! > > (There was just a big discussion on Ruby performance, or lack thereof. > Google "Performance diffrence between ifs and case", and learn A> > premature optimization is the root of all evil, and B> you can profile > each system with unit tests and Benchmark library.) > > In this case it's hash lookup vs. case scanning which is significantly different than if vs. case. Since hash lookups should be O(1) and scanning all the clauses of a case statement is O(n), using the hash would in theory be faster. However, if you're only doing it a couple times and there's a small number of cases, the overhead of creating the Hash might make the case statement faster. In fact, constructing the hash the first time will be at least O(n), so the hash method will almost definitely be slower if you only do the lookup once. Of course, as you said, the best way to find out is to benchmark it yourself. --------------030703000704080507030407--