From: Florian Frank Date: 2005-06-22T05:40:14+09:00 Subject: Re: 1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds 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? > > Your programs aren't the same. The Java and Perl program don't use objects, the Ruby version does. Both the Perl and the Java version could overflow, if numbers get too big, the Ruby program doesn't suffer from this. And look, I just made the Ruby version, faster, smaller and more object oriented: class Integer def prime? not (2..Math.sqrt(self).floor).find { |i| self % i == 0 } end end ######### star here:How many primes in 2 to 50000? start_number, max_number = 2, 50000 time = Time.now.to_f total = (start_number..max_number).inject(0) { |s,i| s + (i.prime? ? 1 : 0) } time = Time.now.to_f - time puts "There are #{total} primes between #{start_number} and #{max_number}" puts "Time taken %.3f seconds" % time Now do the same in the other two languages... -- Florian Frank