Skip to content

Instantly share code, notes, and snippets.

@ihower
Created November 4, 2010 08:37
Show Gist options
  • Save ihower/662236 to your computer and use it in GitHub Desktop.
Save ihower/662236 to your computer and use it in GitHub Desktop.
require 'benchmark'
class User
attr_accessor :a, :b, :c
def attribute_names
["a","b","c"]
end
end
u = User.new
u.a = "AAAAAA"
u.b = "BBBBBB"
u.c = "CCCCCC"
n = 10000000
Benchmark.bm do |x|
x.report("A") {
h = {}
u.attribute_names.each { |a| h[a] = u.send(a) }
}
x.report("B") {
u.attribute_names.inject(Hash.new){ |h,j| h[j]= u.send(j); h }
}
x.report("C") {
Hash[*u.attribute_names.map { |i| [i, u.send(i)] }.flatten]
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment