Last active
August 29, 2015 14:01
-
-
Save seanchas116/d27c8ce9644d03ad515a to your computer and use it in GitHub Desktop.
A ruby benchmark to simulate method lookup slowdown by dynamic method definition (in Ruby 2.1.2)
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' | |
class Test | |
mcount = 1000 | |
mcount.times do |i| | |
class_eval "def method#{i}; end" | |
end | |
class_eval "def many_method_calls; #{ mcount.times.map { |i| "method#{i};" }.join } end" | |
end | |
class Test2; end | |
n = 10000 | |
t = Test.new | |
Benchmark.bm do |b| | |
b.report 'method definition only' do | |
n.times do |i| | |
Test2.class_eval "def test#{i}; end" | |
end | |
end | |
b.report 'method definition and call' do | |
n.times do |i| | |
Test2.class_eval "def test#{i}; end" | |
t.many_method_calls | |
end | |
end | |
b.report 'call only' do | |
n.times do |i| | |
t.many_method_calls | |
end | |
end | |
end |
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
user system total real | |
method definition only 0.190000 0.010000 0.200000 ( 0.201972) | |
method definition and call 0.860000 0.000000 0.860000 ( 0.858854) | |
call only 0.640000 0.000000 0.640000 ( 0.647640) |
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' | |
class Test | |
mcount = 1000 | |
mcount.times do |i| | |
class_eval "def method#{i}; end" | |
end | |
class_eval "def many_method_calls; #{ mcount.times.map { |i| "method#{i};" }.join } end" | |
end | |
n = 10000 | |
t = Test.new | |
Benchmark.bm do |b| | |
b.report 'method definition only' do | |
n.times do |i| | |
Test.class_eval "def test#{i}; end" | |
end | |
end | |
b.report 'method definition and call' do | |
n.times do |i| | |
Test.class_eval "def test#{i}; end" | |
t.many_method_calls | |
end | |
end | |
b.report 'call only' do | |
n.times do |i| | |
t.many_method_calls | |
end | |
end | |
end |
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
user system total real | |
method definition only 0.190000 0.010000 0.200000 ( 0.194328) | |
method definition and call 1.690000 0.010000 1.700000 ( 1.706964) | |
call only 0.600000 0.000000 0.600000 ( 0.601261) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment