require 'benchmark' $ThreadMain = {} class Thread def self.main; $ThreadMain; end def self.current; $ThreadMain; end end require 'contextr' class Foo layer :one layer :two def ordinary true end def once true end def twice true end def wrapped true end def claimed true end one.pre :once do true end one.pre :twice do true end two.pre :twice do true end one.wrap :wrapped do |n| n.call_next true end one.claim :claimed end f = Foo.new n = 100_000 Benchmark.bm(20) do |x| x.report("Ordinary:") { n.times { f.ordinary } } x.report("Once (w/o):") { n.times { f.once } } x.report("Once (ctx):") { ContextR.with_layers :one do n.times { f.once } end } x.report("Twice (w/o):") { n.times { f.twice } } x.report("Twice (ctx):") { ContextR.with_layers :one, :two do n.times { f.twice } end } x.report("Wrapped (w/o):") { n.times { f.wrapped } } x.report("Wrapped (ctx):") { ContextR.with_layers :one, :two do n.times { f.wrapped } end } x.report("Claimed (ctx):") { ContextR.with_layers :one do n.times { f.claimed } end } end __END__ n = 100_000 user system total real Ordinary: 0.230000 0.000000 0.230000 ( 0.538866) Once (w/o): 4.530000 0.060000 4.590000 ( 11.170911) Once (ctx): 5.440000 0.040000 5.480000 ( 13.549051) Twice (w/o): 4.670000 0.050000 4.720000 ( 11.983466) Twice (ctx): 6.690000 0.050000 6.740000 ( 16.513175) Wrapped (w/o): 5.260000 0.040000 5.300000 ( 13.206653) Wrapped (ctx): 11.610000 0.120000 11.730000 ( 32.038749)