Skip to content

Instantly share code, notes, and snippets.

@mculp
Created January 21, 2012 18:08
Show Gist options
  • Select an option

  • Save mculp/1653459 to your computer and use it in GitHub Desktop.

Select an option

Save mculp/1653459 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'forgery'
def new_ostruct_member(arg); end
def generate_data
(0..1000).map do |i|
{ "k#{i}" => "v#{i}" }
end.inject &:merge
end
@data = generate_data
@table = {}
@ntable = {}
Benchmark.bmbm do |x|
x.report("old") { 1000.times {
for k,v in @data
@table[k.to_sym] = v
new_ostruct_member(k)
end
} }
x.report("new") { 1000.times {
@data.each do |key, value|
@ntable[key.to_sym] = value
new_ostruct_member key
end
} }
end
Rehearsal ---------------------------------------
old 1.660000 0.010000 1.670000 ( 1.679527)
new 1.070000 0.000000 1.070000 ( 1.102152)
------------------------------ total: 2.740000sec
user system total real
old 1.650000 0.010000 1.660000 ( 1.667362)
new 1.040000 0.010000 1.050000 ( 1.064195)
@krainboltgreene
Copy link

I added in the if and @table to best mimic the benchmarked method, and because they're relatively the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment