Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created September 28, 2012 17:46
Show Gist options
  • Select an option

  • Save jonleighton/3801227 to your computer and use it in GitHub Desktop.

Select an option

Save jonleighton/3801227 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
class A
def initialize
@attributes = { :foo => 1 }
end
def foo
@attributes[:foo]
end
end
class B
def initialize
@attributes = { 'foo' => 1 }
end
def foo
@attributes['foo']
end
end
a = A.new
b = B.new
Benchmark.ips do |r|
r.report('symbol') { a.foo }
r.report('string') { b.foo }
end
$ ruby benchmark_access.rb
Calculating -------------------------------------
symbol 49716 i/100ms
string 45447 i/100ms
-------------------------------------------------
symbol 2495270.4 (±4.5%) i/s - 12478716 in 5.014637s
string 1645124.6 (±8.2%) i/s - 8180460 in 5.012095s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment