From: Ara.T.Howard@... Date: 2005-06-22T06:30:36+09:00 Subject: Re: 1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds On Wed, 22 Jun 2005, Michael Tan wrote: > --0-1157793495-1119383752=:96009 > Content-Type: text/plain; charset=iso-8859-1 > Content-Transfer-Encoding: 8bit > > 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 if you just want a fast ruby version: harp:~ > cat prime_inline.rb def benchmark label, code STDOUT.sync = true puts "#{ '=' * 16 }\n#{ label }\n#{ '=' * 16 }" fork do GC.disable a = Time::now.to_f code.call b = Time::now.to_f puts " @ #{ b - a }" end Process::wait end prime_test = lambda{ (2 ... (2 ** 14)).each{|n| n.prime?} } class Fixnum def prime? (2...self).each{|i| return false if self % i == 0} return true end end benchmark 'pure ruby', prime_test require 'inline' class Fixnum inline do |builder| builder.c_raw <<-src static VALUE is_prime_c (int argc, VALUE *argv, VALUE self) { long i, n = FIX2LONG (self); for (i = 2; i < n; i++) if (n % i == 0) return Qfalse; return Qtrue; } src end alias prime? is_prime_c end benchmark 'inline c', prime_test harp:~ > ruby prime_inline.rb ================ pure ruby ================ @ 14.9205560684204 ================ inline c ================ @ 0.539831876754761 so that's about two orders of magnitude speed-up for less than 10 extra lines of code - and you still get nice things like overflow safety. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================