From: Reid Thompson Date: 2012-02-03T13:10:14+09:00 Subject: Re: Basic Ruby performance On 2/2/2012 5:20 PM, Dmitry Nikiforov wrote: > Hello all! > > First, I should point out that I'm new to Ruby, although it seems pretty > similar in some regards to JavaScript and Perl. > > Anyway, I'm not sure if it's normal, or if it's specifics of Ruby on Mac > OS X, or if I haven't compiled it properly (although I used rvm to > install on my OS X Lion, to have the latest version - Lion comes with > 1.8.7 by default, I believe, so I installed 1.9.3), but most things I > try to replicate with it that I used Perl to do before run about twice > slower. So I ran some basic benchmarks. Here's one example: > > Ruby: > > for a in 0..1E8 > a*2 > end > > Perl: > > for $a ( 0..1E8 ) { > a*2 > } > > Ruby takes 22 seconds, Perl - 9 seconds to execute this. This is very > similar to all other scenarios I tried (one of which is splitting > millions of comma separated rows into arrays). > > I would really appreciate any useful suggestions: I would LOVE to be > able to use Ruby for most of the stuff I do (it's not that I don't like > Perl, but I love Ruby's syntax :) ) > > Thanks! > $ cat mult.rb #for a in 0..100000000 # a*2 #end require 'rubygems' require 'inline' class Multiply inline do |builder| builder.c " long mult(int max) { long ctr = 0; unsigned long long result; while (ctr < max){ result = (ctr++ * 2);} return result; }" end end # puts ARGV[0] m = Multiply.new() start_time = Time.now a = m.mult(ARGV[0].to_i) puts a.to_s end_time = Time.now duration = ((end_time.to_f - start_time.to_f) * 1000.0).to_i puts "You took " + duration.to_s + " seconds." my linux box model name : Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz quad core $ ruby -v ruby 1.8.7 (2011-12-28 patchlevel 357) [x86_64-linux] [23:05:57] rthompso@raker2>~ $ time ruby mult.rb 999999991 999999991 1999999980 You took 0 seconds. real 0m0.059s user 0m0.050s sys 0m0.008s my windows xp laptop using cygwin ruby T7100 @ 1.80GHZ dual core Reid.Thompson@lt-lat-4960 ~ $ ruby -v ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] Reid.Thompson@lt-lat-4960 ~ $ time ruby rbtest.rb 999999991 999999991 1999999980 You took 0 seconds. real 0m0.303s user 0m0.109s sys 0m0.170s