Month: February 2010

slow, slower, activerecord-jdbcsqlite3-adapter

Posted by on February 9, 2010

I did a little performance test to find out which database to use in a JRuby desktop application. To have a comparison value I also ran the test (insert, find+update, destroy each 500 times) with MRI and the native sqlite3 driver. It revealed that the jdbcsqlite3 adapter is really sloooooooooow:

$ ruby test.rb sqlite3

                 user     system   total     real
insert           0.4500   0.0900   0.5400 (  7.303116)
find and update  0.4900   0.1400   0.6300 (  6.984390)
destroy          0.3300   0.1100   0.4400 (  7.261199)

$ jruby test.rb jdbcsqlite3

                 user     system   total     real
insert          17.2500   0.0000  17.2500 ( 17.250000)
find and update 16.3270   0.0000  16.3270 ( 16.327000)
destroy         15.0900   0.0000  15.0900 ( 15.089000)

$ jruby test.rb h2

                 user     system   total     real
insert           1.9320   0.0000   1.9320 (  1.932000)
find and update  0.7800   0.0000   0.7800 (  0.780000)
destroy          0.3180   0.0000   0.3180 (  0.318000)

That’s what I call an easy decision.

My setup can be downloaded here.

Difference between do-end and {} blocks

Posted by on February 7, 2010

There is a important difference between the two block syntaxes in Ruby: Their precedence.
Consider following code:

def method1(*args)
  puts "method1 got a block" if block_given?
end
 
def method2(*args)
  puts "method2 got a block" if block_given?
end
 
method1 method2 do
end
 
method1 method2 {
}

You would expect both method calls to produce the same result, wouldn’t you?
Output:

method1 got a block
method2 got a block

Obviously the do-end block is passed to the first method in the expression while the {} block is passed to the method called directly before it.
Another example shows that the assignment does not count as a method call here:

#[...]
class Foo
  def bar=(o)
    puts "Foo#bar= got a block" if block_given?
  end
end
foo = Foo.new
 
foo.bar = method1 method2 do
end 
#method1 got a block
 
foo.bar = method1 method2 {
} 
#method2 got a block