Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
instance_variable_get performance
require 'benchmark/ips'
class Test
def intitialize
@ivar = 'test'
end
def direct
@ivar
end
IVAR_NAME = "@ivar"
def hoist_ivar_name
instance_variable_get IVAR_NAME
end
def instance_variable_get_symbol
instance_variable_get :"@ivar"
end
def instance_variable_get_frozen
instance_variable_get('@ivar'.freeze)
end
def instance_variable_get_frozen_symbol
instance_variable_get('@ivar'.freeze.to_sym)
end
end
Benchmark.ips do |benchmark|
instance = Test.new
benchmark.report("direct") {instance.direct}
benchmark.report("hoist_ivar_name") {instance.hoist_ivar_name}
benchmark.report("ivar get symbol") {instance.instance_variable_get_symbol}
benchmark.report("ivar get frozen") {instance.instance_variable_get_frozen}
benchmark.report("ivar get frozen sym") {instance.instance_variable_get_frozen_symbol}
end
@PragTob

This comment has been minimized.

Copy link
Owner Author

@PragTob PragTob commented Oct 23, 2015

ruby 2.2.3:

tobi@airship ~ $ ruby ivar_get.rb 
Calculating -------------------------------------
              direct   175.604k i/100ms
     hoist_ivar_name   125.430k i/100ms
     ivar get symbol   139.701k i/100ms
     ivar get frozen   138.203k i/100ms
 ivar get frozen sym   142.130k i/100ms
-------------------------------------------------
              direct      8.253M (± 8.7%) i/s -     40.740M
     hoist_ivar_name      4.487M (±13.2%) i/s -     21.825M
     ivar get symbol      6.977M (±10.2%) i/s -     34.366M
     ivar get frozen      4.707M (± 1.7%) i/s -     23.633M
 ivar get frozen sym      4.671M (±10.9%) i/s -     22.883M
@PragTob

This comment has been minimized.

Copy link
Owner Author

@PragTob PragTob commented Oct 23, 2015

jruby 9.0.3.0:

tobi@airship ~ $ ruby ivar_get.rb 
Calculating -------------------------------------
              direct   150.109k i/100ms
     hoist_ivar_name   140.955k i/100ms
     ivar get symbol   192.115k i/100ms
     ivar get frozen   158.494k i/100ms
 ivar get frozen sym   148.150k i/100ms
-------------------------------------------------
              direct     10.335M (±22.3%) i/s -     48.185M
     hoist_ivar_name      4.713M (±28.6%) i/s -     21.425M
     ivar get symbol      8.628M (±27.8%) i/s -     39.191M
     ivar get frozen      3.109M (± 4.7%) i/s -     15.532M
 ivar get frozen sym      5.500M (±15.0%) i/s -     26.519M
@PragTob

This comment has been minimized.

Copy link
Owner Author

@PragTob PragTob commented Oct 23, 2015

jruby 9.0.3.0 with some optimizations turned on:

tobi@airship ~ $ ruby --server -Xcompile.invokedynamic=true ivar_get.rb 
Calculating -------------------------------------
              direct    91.369k i/100ms
     hoist_ivar_name   112.476k i/100ms
     ivar get symbol   113.564k i/100ms
     ivar get frozen   160.592k i/100ms
 ivar get frozen sym   158.691k i/100ms
-------------------------------------------------
              direct     10.812M (±19.7%) i/s -     50.162M
     hoist_ivar_name      5.402M (±28.8%) i/s -     24.070M
     ivar get symbol     10.310M (±12.7%) i/s -     49.968M
     ivar get frozen      3.163M (± 5.0%) i/s -     15.899M
 ivar get frozen sym      5.428M (±17.5%) i/s -     26.025M
@justinjdickow

This comment has been minimized.

Copy link

@justinjdickow justinjdickow commented Jun 28, 2020

ruby 2.6.0 (and newer hardware)

Warming up --------------------------------------
              direct     2.415M i/100ms
     hoist_ivar_name     1.101M i/100ms
     ivar get symbol     1.821M i/100ms
     ivar get frozen     1.136M i/100ms
 ivar get frozen sym     1.116M i/100ms
Calculating -------------------------------------
              direct     24.245M (± 0.9%) i/s -    123.177M in   5.080832s
     hoist_ivar_name     10.951M (± 0.6%) i/s -     55.056M in   5.027655s
     ivar get symbol     18.150M (± 1.0%) i/s -     91.067M in   5.017868s
     ivar get frozen     11.174M (± 3.1%) i/s -     56.781M in   5.086655s
 ivar get frozen sym     10.308M (± 5.6%) i/s -     52.456M in   5.106387s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment