Last active
December 17, 2015 20:09
-
-
Save jmazzi/5665375 to your computer and use it in GitHub Desktop.
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' | |
def resolve(obj) | |
obj.respond_to?(:call) ? resolve(obj.call) : obj | |
end | |
def resolve2(obj) | |
if obj.respond_to?(:call) | |
resolve2(obj.call) | |
else | |
obj | |
end | |
end | |
def resolve3(obj) | |
while obj.respond_to?(:call) | |
obj = obj.call | |
end | |
obj | |
end | |
open = 1500.times.map { "proc {" }.join(" ") | |
close = 1500.times.map { "}" }.join(" ") | |
f = eval(open + "Time.now" + close) | |
Benchmark::bm do |x| | |
x.report { resolve(f) } | |
x.report { resolve2(f) } | |
x.report { resolve3(f) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment