Archive for the ‘script’ Category

snippet : A benchmark in ruby

An easy snippets thats show how to do a benchmark :

require 'benchmark'

require 'base64'Benchmark.bm(8) do |x|

  n   = 1_000_000

  hsh = { :id => 1, :name => 'Foo', :description => 'bar',

          :created_at => '2007-06-01 16:05:41.592507',

          :updated_at => '2007-06-01 16:05:41.592507', }

  str = Base64::encode64(Marshal.dump(hsh))

  x.report('delete!:') { n.times { str.dup.delete!("n") << "n" } }

  x.report('gsub!  :') { n.times { str.dup.gsub!(/n(?=.)/, '') } }

  x.report('linear :') { n.times { str = str.dup; str.gsub!(/n(?=.)/, '') } }

  x.report('scan   :') { n.times { str.dup.scan(%r|.{1,60}|).join } }

end

via pastie.

Follow

Get every new post delivered to your Inbox.