tycooon / memery

A gem for memoization in Ruby
MIT License
177 stars 13 forks source link

Optimize speed and memory for cached values returns #10

Closed AlexWayfer closed 5 years ago

AlexWayfer commented 5 years ago

require 'bundler/setup'
Bundler.setup

require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'

puts '```ruby'
puts File.read(__FILE__).gsub("\t", '  ')
puts '```'
puts
puts '### Output'
puts
puts '```'

require_relative 'lib/memery'
require_relative 'lib/memery_new'

class Foo
  class << self
    include Memery
    include MemeryNew

    def base_find(char)
      ('a'..'k').find { |letter| letter == char }
    end

    memoize def find_old(char)
      base_find(char)
    end

    memoize_new def find_new(char)
      base_find(char)
    end
  end
end

def memery_old
  Foo.find_old('d')
end

def memery_new
  Foo.find_new('d')
end

def test
  exit unless p (((p memery_old) == (p memery_new)))
end

test

Benchmark.ips do |x|
  x.report('memery_old') { memery_old }
  x.report('memery_new') { memery_new }

  x.compare!
end

Benchmark.memory do |x|
  x.report('memery_old') { 100.times { memery_old } }
  x.report('memery_new') { 100.times { memery_new } }

  x.compare!
end

puts '```'
"d"
"d"
true
Warming up --------------------------------------
          memery_old    24.664k i/100ms
          memery_new    31.363k i/100ms
Calculating -------------------------------------
          memery_old    285.113k (± 5.7%) i/s -      1.431M in   5.034622s
          memery_new    360.540k (± 6.2%) i/s -      1.819M in   5.067173s

Comparison:
          memery_new:   360540.0 i/s
          memery_old:   285112.6 i/s - 1.26x  slower

Calculating -------------------------------------
          memery_old    32.800k memsize (     0.000  retained)
                       500.000  objects (     0.000  retained)
                         3.000  strings (     0.000  retained)
          memery_new    16.000k memsize (     0.000  retained)
                       400.000  objects (     0.000  retained)
                         3.000  strings (     0.000  retained)

Comparison:
          memery_new:      16000 allocated
          memery_old:      32800 allocated - 2.05x more
coveralls commented 5 years ago

Pull Request Test Coverage Report for Build 33


Totals Coverage Status
Change from base Build 31: 0.0%
Covered Lines: 152
Relevant Lines: 152

💛 - Coveralls
AlexWayfer commented 5 years ago

Could you please provide the whole benchmark script?

I have provided. Memery — the file from master, MemeryNew — renamed version from this PR.

swrobel commented 5 years ago

@tycooon is this in a review-able state now?

swrobel commented 5 years ago

FYI, I benchmarked memery vs Memoist. With the published gem version, memery is 1.32x slower & allocates 1.92x more. With this fix, it's roughly identical:

Comparison:
             memoist:   417894.3 i/s
              memery:   405734.6 i/s - same-ish: difference falls within error
             memoist:   192 allocated
              memery:   200 allocated - 1.04x more