NOTE: This is an archive of my old blog. Go to for my current website.

Ruby for Chumby: HowTo

Posted by md on June 02, 2010

It took me a while to figure this out, so here are my notes:
Continue reading…

Ruby 1.9 Performance

Posted by md on April 17, 2009

I’m currently using Ruby to optimize schedules based on a simulated annealing approach. For my current intermediate version I rely on marshalling internal datastructures really frequently. Out of curiosity I compared the runtimes of Ruby 1.8 and 1.9:

  • Ruby 1.8.6: 21 minutes, 55 seconds
  • Ruby 1.9.0: 22 minutes, 13 seconds

I certainly did not expect any wonders, but in theis case, the runtimes are about the same. So I’m investigating further, using the ruby 1.8 profiler on a smaller test problem:

% cumulative self self total
time seconds seconds calls ms/call ms/call name
33.20 32.96 32.96 661 49.86 77.25 Marshal.load
17.80 50.63 17.67 6365 2.78 14.96 Array#each
17.52 68.03 17.40 601659 0.03 0.03 IO#getc
9.19 77.15 9.12 660 13.82 13.82 Marshal.dump
6.01 83.12 5.97 187158 0.03 0.03 Float#+

My code spends 42.39 percent of its runtime doing marshalling operations. For Ruby 1.9, the code spends 48.98 percent of its runtime doing marshalling. This is somewhat disappointing. In a second round I changed my code to not relying on marshalling – basically, I am using clone on essential internal datastructures. All other information can be reconstructed afterwards. This means that I need to recompute certain values, but evidently this is a much faster approach:

  • Ruby 1.8.6: 1 minute, 34 seconds
  • Ruby 1.9.0: 1 minute, 17 seconds

Note to self: always use a profiler before refactoring code for performance reasons.

Generating random numbers from any distribution

Posted by md on July 29, 2008

At work I needed to generate random numbers following a combination of two gaussian distributions – which gave me some headache until someone pointed me to using a Monte Carlo approach. Here’s how.

Continue reading…