I was looking for something to run to try to put the current Ruby 1.9 through its paces. The first code I found was this
total = 0.0 1.0.step(2000.0,0.0001) do |x| result = (5.4*x**5 - 3.211*x**4 + 100.3*x**2 - 100 + 20*Math.sin(x) - Math.log(x)) * 20*Math.exp(-x/100.3) total += result / 0.0001 end puts total
Its heavy on the floating point that’s what I mostly use; ‘normal’ use is more likely integer math. Its a nonsensical calculation that I should rework as a real integration problem…
Timings (intel iMac)
imac:$ time ruby pure_ruby.rb 1.31784023450574e+24 real 1m43.283s user 1m38.615s sys 0m1.213s imac:$ time /usr/local/ruby1.9/bin/ruby pure_ruby.rb 1.31784023450574e+24 real 1m1.352s user 0m58.550s sys 0m0.749s
Which is 1.7x faster. Not bad for no additional work. These numbers are very preliminary, and shouldn’t be mistaken for real benchmarks. I have a series of tests I did with pair correlation function calculations on a simulated liquid that I want to rerun and see how 1.9 holds up.
I thought I’d try the numbers under JRuby and my trunk Ruby 1.9 too:
~/NetBeansProjects/jruby $ time jruby -J-server test.rb
1.3178402345057376e+24
real 1m7.536s
user 1m5.026s
sys 0m2.143s
~/NetBeansProjects/jruby $ time ../ruby1.9/ruby test.rb
1.31784023450574e+24
real 1m8.436s
user 1m7.388s
sys 0m0.358s
JRuby comes out a little faster here, but I think it can be improved more.
All the posts on this site are really good. In fact just what I was looking for. I’m also a research assistant at a university, but I have been using python. Are you still using ruby in your research? I see it’s been a year since you posted, I hope you have not lost interest.
Have you tried with more recent versions of Ruby or JRuby? Performance is supposed to have improved even more. Also, what are your favorite libraries for doing numerical work in Ruby?