Raymond Law
Badminton on Rails
-
Ruby Performance Benchmarking
Posted on July 9th, 2008 No commentsCourtesy of igvita.com
require 'benchmark' n = 1000000 Benchmark.bm do |x| x.report('copy') { n.times do ; h = {}; h = h.merge({1 => 2}); end } x.report('no copy') { n.times do ; h = {}; h.merge!({1 => 2}); end } x.report('map(&:id)') do n.times do |i| h = {} h.map(&:object_id) end end x.report('map { |i| i.id }') do n.times do |i| h = {} h.map { |e| e.object_id } end end end
In Rails script/console or Ruby 1.9:
>> load '/Users/rlaw/Desktop/bm.rb' user system total real copy 4.000000 0.020000 4.020000 ( 4.081549) no copy 2.780000 0.010000 2.790000 ( 2.818923) map(&:id) 10.880000 0.040000 10.920000 ( 11.094024) map { |i| i.id } 2.060000 0.020000 2.080000 ( 2.178544) => []
Leave a reply



Recent Comments