Skip to content

Instantly share code, notes, and snippets.

@jmazzi
Last active December 17, 2015 20:09
Show Gist options
  • Save jmazzi/5665375 to your computer and use it in GitHub Desktop.
Save jmazzi/5665375 to your computer and use it in GitHub Desktop.
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