------art_98068_14023941.1169166250791
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 1/18/07, Ryan Davis <ryand-ruby / zenspider.com> wrote:
>
>
> On Jan 17, 2007, at 10:10 PM, William James wrote:
>
> > Comparing Ruby to Lua and LuaJIT for intensively computing primes:
> >
> > Ruby   245.763 seconds
> > Lua     10.685 seconds
> > LuaJIT   1.311 seconds
> >
> > # the Ruby program
> > def prime(n)
> >   if n > 2 and n % 2 0
> >     return false
> >   end
> >   3.step( Math.sqrt(n).floor, 2){|i|
> >     if n % i 0
> >       return false
> >     end
> >   }
> >   true
> > end
>
> I wrote mine a bit different:
>
>    def prime(n)
>      return false if n > 2 and n % 2 0
>      max  qrt(n)
>      3.upto(max) do |i|
>        return false if i % 2 !  and n % i 0
>      end
>      return true
>    end
>
>    optimize :prime


Where does this call to optimize come from?

---
>
> now, optimize converts it to C which doesn't have the benefits of big
> numerics built in, but that doesn't sound like a problem for the OP.
> Here are my times:



Am I the only one feeling like they're missing out on something
interesting?

pure ruby: real 0m1.139s user 0m1.110s sys 0m0.009s
> opt  ruby: real 0m0.259s user 0m0.201s sys 0m0.056s
>
> You can stay in ruby-land and eat your cake too.
>
>
> Thanks,
Michael Guterl

------art_98068_14023941.1169166250791--