-
-
Save kinduff/24b8342cea250cbb6aa96b629674ce56 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' | |
class PersonClass | |
attr_accessor :name, :age | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
end | |
person = PersonClass.new("John", 30) | |
hash = { name: "John", age: 30 } | |
Benchmark.bmbm do |x| | |
x.report("Class attribute access") do | |
1_000_000.times do | |
person.name | |
person.age | |
end | |
end | |
x.report("Hash key access") do | |
1_000_000.times do | |
hash[:name] | |
hash[:age] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby 3.2.2
With GC disabled:
Without GC disabled: