-
-
Save LTe/5574956 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' | |
s1 = Struct.new(:login, :password).new("login", "password") | |
class S | |
attr_accessor :login, :password | |
def initialize(login, password) | |
@login = login | |
@password = password | |
end | |
end | |
s2 = S.new("login", "password") | |
times = 100_000_000 | |
Benchmark.bmbm do |b| | |
b.report("Struct") do | |
times.times do | |
s1.login | |
s1.password | |
end | |
end | |
b.report("attr") do | |
times.times do | |
s2.login | |
s2.password | |
end | |
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 ------------------------------------------ | |
Struct 12.930000 0.000000 12.930000 ( 12.943928) | |
attr 10.280000 0.000000 10.280000 ( 10.276997) | |
-------------------------------- total: 23.210000sec | |
user system total real | |
Struct 12.850000 0.000000 12.850000 ( 12.858140) | |
attr 10.330000 0.000000 10.330000 ( 10.335870) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment