Skip to content

Instantly share code, notes, and snippets.

Created February 1, 2013 15:26
Show Gist options
  • Save anonymous/4691954 to your computer and use it in GitHub Desktop.
Save anonymous/4691954 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
class Test
attr_reader :instance_var
def initialize
@instance_var = 'value'
end
end
object = Test.new
Benchmark.ips do |x|
x.report { object.instance_var }
x.report { object.instance_variable_get :@instance_var }
end
# $ ruby -v
# ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.0.0]
# $ ruby ivar_benchmark.rb
# Calculating -------------------------------------
# 42823 i/100ms
# 51152 i/100ms
# -------------------------------------------------
# 2603014.6 (±3.9%) i/s - 13018192 in 5.011281s
# 2372758.6 (±2.4%) i/s - 11867264 in 5.005498s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment