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.

Advertisements

1 comment so far

  1. العاب on

    you will have an excellent weblog right here! would you like to make some invite posts on my blog?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: