On Jun 21, 2005, at 12:55 PM, Michael Tan wrote:

> Just new to Ruby since last week, running  my same functional  
> program on the windows XP(Pentium M1.5G), the Ruby version is 10  
> times slower than the Java version. The program is to find the  
> prime numbers like 2, 3,5, 7, 11, 13...  Are there setup issues? or  
> it is normal?
> 1. Ruby result:  101 seconds
> 2. Java result:9.8 seconds
> 3. Perl result:62 seconds

With some very minor modifications to the original code (I did upto  
instead of for and wrapped is_prime in a class), none of which were  
algorithmic improvements (ugh):

Modified is_prime code:

class Primer
   def is_prime(number)
     2.upto(number-1) do |i|
       return false if number % i == 0
     end
     return true
   end
end

NORMAL:

% rm -rf ~/.ruby_inline/; ruby primes.rb 50000
There are 5133 primes between 2 and 50000
Time taken 237.315160036087 seconds

     (whoa. my laptop is a slowpoke! oh yeah, I'm on battery!)

ONE EXTRA CMD-LINE TOKEN:

% rm -rf ~/.ruby_inline/; ruby -rzenoptimize primes.rb 50000
*** Optimizer threshold tripped!! Optimizing Primer.is_prime
There are 5133 primes between 2 and 50000
Time taken 2.81669783592224 seconds

That said... what did we learn from this thread?

     Yes... benchmarks are dumb (see also, 3 lines down)
         That there is no accounting for taste (ugliest code/ 
algorithm ever).
             A good algorithm goes a long way.
                 2/3rds of the ppl are going to mis-analyze anyhow.

... Nothing really.



--
ryand-ruby / zenspider.com - Seattle.rb - http://www.zenspider.com/ 
seattle.rb
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c