Skip to content

Instantly share code, notes, and snippets.

@joshjordan
Forked from gamache/method-cache-test.rb
Last active December 25, 2015 10:49
Show Gist options
  • Select an option

  • Save joshjordan/6964176 to your computer and use it in GitHub Desktop.

Select an option

Save joshjordan/6964176 to your computer and use it in GitHub Desktop.

Revisions

  1. joshjordan revised this gist Oct 13, 2013. 1 changed file with 48 additions and 22 deletions.
    70 changes: 48 additions & 22 deletions method-cache-test.rb
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,46 @@
    require 'benchmark'

    module A
    end

    class DefinedMethodStyle
    def bust_cache(arg)
    def dont_bust_cache(*)
    Object.new
    nil
    end

    def bust_cache_class(*)
    Class.new
    arg
    nil
    end

    def dont_bust_cache(arg)
    Object.new
    arg
    def bust_cache_extend(*)
    Object.new.extend(A)
    nil
    end
    end

    class MethodMissingStyle
    def _bust_cache(arg)
    def _dont_bust_cache(*)
    Object.new
    nil
    end

    def _bust_cache_class(*)
    Class.new
    arg
    nil
    end

    def _dont_bust_cache(arg)
    Object.new
    arg
    def _bust_cache_extend(*)
    Object.new.extend(A)
    nil
    end

    def method_missing(method, *args)
    if method == :bust_cache
    _bust_cache(*args)
    if method == :bust_cache_class
    _bust_cache_class(*args)
    elsif method == :bust_cache_extend
    _bust_cache_extend(*args)
    elsif method == :dont_bust_cache
    _dont_bust_cache(*args)
    end
    @@ -36,16 +51,27 @@ def method_missing(method, *args)
    mms = MethodMissingStyle.new
    n = 1_000_000

    Benchmark.bm do |bm|
    puts "\n defined methods, not busting cache:"
    bm.report { for i in 1..n; dms.dont_bust_cache(i) end }
    puts "Controls"
    controls = Benchmark.bm do |bm|
    bm.report("Object.new") { n.times { Object.new } }
    bm.report("Class.new") { n.times { Class.new } }
    bm.report("Object.new.extend(A)") { n.times { Object.new.extend(A) } }
    end

    puts "\n method_missing dispatch, not busting cache:"
    bm.report { for i in 1..n; mms.dont_bust_cache(i) end }
    puts "Tests"
    tests = Benchmark.bm do |bm|
    bm.report("defined methods, not busting cache") { n.times { |i| dms.dont_bust_cache(i) } }
    bm.report("method_missing dispatch, not busting cache") { n.times { |i| mms.dont_bust_cache(i) } }
    bm.report("defined methods, busting cache with Class.new") { n.times { |i| dms.bust_cache_class(i) } }
    bm.report("method_missing dispatch, busting cache with Class.new") { n.times { |i| mms.bust_cache_class(i) } }
    bm.report("defined methods, busting cache with Object.new.extend(A)") { n.times { |i| dms.bust_cache_extend(i) } }
    bm.report("method_missing dispatch, busting cache with Object.new.extend(A)") { n.times { |i| mms.bust_cache_extend(i) } }
    end

    puts "\n defined methods, busting cache:"
    bm.report { for i in 1..n; dms.bust_cache(i) end }
    puts "Adjusted for overhead"
    tests.each do |test|
    puts test.label

    puts "\n method_missing dispatch, busting cache:"
    bm.report { for i in 1..n; mms.bust_cache(i) end }
    end
    control = controls[tests.find_index(test) / 2]
    puts "Approximate method resolution time: #{test.real - control.real}"
    end
  2. @gamache gamache created this gist Oct 13, 2013.
    51 changes: 51 additions & 0 deletions method-cache-test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    require 'benchmark'

    class DefinedMethodStyle
    def bust_cache(arg)
    Class.new
    arg
    end

    def dont_bust_cache(arg)
    Object.new
    arg
    end
    end

    class MethodMissingStyle
    def _bust_cache(arg)
    Class.new
    arg
    end

    def _dont_bust_cache(arg)
    Object.new
    arg
    end

    def method_missing(method, *args)
    if method == :bust_cache
    _bust_cache(*args)
    elsif method == :dont_bust_cache
    _dont_bust_cache(*args)
    end
    end
    end

    dms = DefinedMethodStyle.new
    mms = MethodMissingStyle.new
    n = 1_000_000

    Benchmark.bm do |bm|
    puts "\n defined methods, not busting cache:"
    bm.report { for i in 1..n; dms.dont_bust_cache(i) end }

    puts "\n method_missing dispatch, not busting cache:"
    bm.report { for i in 1..n; mms.dont_bust_cache(i) end }

    puts "\n defined methods, busting cache:"
    bm.report { for i in 1..n; dms.bust_cache(i) end }

    puts "\n method_missing dispatch, busting cache:"
    bm.report { for i in 1..n; mms.bust_cache(i) end }
    end