Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am trying to benchmark two cases of self referential many to many as described in datamapper associations. Both cases consist of an Item clss, which may require many other items. In both cases, I required the ruby benchmark library and source file, created two items and benchmarked require/unrequie functions as below:

Benchmark.bmbm do |x|"require:")   { item_1.require_item item_2, 10 }"unrequire:")   { item_1.unrequire_item item_2 }

To be clear, both functions are datamapper add/modify functions like:

componentMaps.create :component_id =>, :quantity => quantity
componentMaps.all(:component_id =>!


links_to_components.create :component_id =>, :quantity => quantity
links_to_components.all(:component_id =>!

The results are variable and in the range of 0.018001 to 0.022001 for require function in both cases, and 0.006 to 0.01 for unrequire function in both cases. This made me suspicious about the correctness of my test method.


I went ahead and compared a "get by primary key case" to a "finding first matching record case" by:

(1..10000).each do |i|
  Item.create :name => "item_#{i}"

Benchmark.bmbm do |x|"Get") { item = Item.get 9712 }"First") { item = Item.first :name => "item_9712" }

where the results were very different like 0 sec compared to 0.0312, as expected. This suggests that the benchmarking works.

I wonder whether I benchmarked the two types of associations correctly, and whether a difference between 0.018 and 0.022 sec significant?

share|improve this question
Not quite sure what you're asking. Getting different performance on laptop vs hosting would be expected due to different hardware. Why would this suggest that your methods are not correct? – Matt Gibson Nov 5 '12 at 15:42
Thank you for pointing that out. I wasn't mentioning the difference between my laptop and hosting. It's my bad that I mentioned this useless info, and I removed it. I am not proficient in SQL, and since results of my benchmark came out tobe very similar, I wonder if I used the correct technique for this comparison. I mean, maybe I needed to compare cases of mass modify instead of simple read/writes to see the real difference. – barerd Nov 5 '12 at 20:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.