Skip to content

Instantly share code, notes, and snippets.

@LTe
Forked from paneq/bm.rb
Created May 14, 2013 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LTe/5574956 to your computer and use it in GitHub Desktop.
Save LTe/5574956 to your computer and use it in GitHub Desktop.
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
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