Skip to content

Instantly share code, notes, and snippets.

@korun
Created April 3, 2015 07:46
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 korun/69a0ed0a89cafda3c269 to your computer and use it in GitHub Desktop.
Save korun/69a0ed0a89cafda3c269 to your computer and use it in GitHub Desktop.
ruby const_get vs ::
require 'benchmark'
n = 1_000_000
class Parent
HANDLER_METHOD = :awesome_method
end
class Children < Parent
HANDLER_METHOD = :not_very_awesome_method
end
ch = Children.new
Benchmark.bmbm 20 do |results|
results.report 'cycle' do
n.times do
end
end
results.report 'class.const_get' do
n.times do
ch.class.const_get(:HANDLER_METHOD)
end
end
results.report 'class::' do
n.times do
ch.class::HANDLER_METHOD
end
end
end
@korun
Copy link
Author

korun commented Apr 3, 2015

$ uname -a && ruby benchmark.rb 
Linux Inspiron-17R 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:44 UTC 2014 i686 i686 i686 GNU/Linux
Rehearsal --------------------------------------------------------
cycle                  0.050000   0.000000   0.050000 (  0.049341)
class.const_get        0.450000   0.000000   0.450000 (  0.448197)
class::                0.100000   0.000000   0.100000 (  0.098225)
----------------------------------------------- total: 0.600000sec

                           user     system      total        real
cycle                  0.050000   0.000000   0.050000 (  0.046460)
class.const_get        0.440000   0.000000   0.440000 (  0.447367)
class::                0.100000   0.000000   0.100000 (  0.093392)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment