Created
January 21, 2012 18:08
-
-
Save mculp/1653459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Jan 21, 2012
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