Created
October 23, 2015 15:47
-
-
Save PragTob/fdeb46356702bf9943ca to your computer and use it in GitHub Desktop.
instance_variable_get performance
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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby 2.6.0 (and newer hardware)