Skip to content

Instantly share code, notes, and snippets.

@swrobel
Created August 3, 2019 01:59
Show Gist options
  • Save swrobel/fa739829d317ff5faa759c9fc5c25428 to your computer and use it in GitHub Desktop.
Save swrobel/fa739829d317ff5faa759c9fc5c25428 to your computer and use it in GitHub Desktop.
Ruby memoization gem benchmarks
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem 'benchmark-ips'
gem 'benchmark-memory'
gem 'memo-it'
gem 'memoist'
gem 'memery'
end
class Base
class << self
def base_find(char)
('a'..'k').find { |letter| letter == char }
end
end
end
class MemeryTest < Base
class << self
include Memery
memoize def find(char)
base_find(char)
end
end
end
class MemoItTest < Base
class << self
include Memery
def find(char)
memo { base_find(char) }
end
end
end
class MemoistTest < Base
class << self
extend Memoist
def find(char)
base_find(char)
end
memoize :find
end
end
Benchmark.ips do |x|
x.report('memery') { MemeryTest.find('f') }
x.report('memo-it') { MemoItTest.find('f') }
x.report('memoist') { MemoistTest.find('f') }
x.compare!
end
Benchmark.memory do |x|
x.report('memery') { MemeryTest.find('f') }
x.report('memo-it') { MemoItTest.find('f') }
x.report('memoist') { MemoistTest.find('f') }
x.compare!
end
Warming up --------------------------------------
memery 24.779k i/100ms
memo-it 11.322k i/100ms
memoist 34.413k i/100ms
Calculating -------------------------------------
memery 317.181k (± 5.8%) i/s - 1.586M in 5.019854s
memo-it 138.049k (± 7.7%) i/s - 690.642k in 5.036792s
memoist 422.394k (± 5.9%) i/s - 2.134M in 5.071838s
Comparison:
memoist: 422394.3 i/s
memery: 317181.0 i/s - 1.33x slower
memo-it: 138049.2 i/s - 3.06x slower
Calculating -------------------------------------
memery 368.000 memsize ( 0.000 retained)
6.000 objects ( 0.000 retained)
4.000 strings ( 0.000 retained)
memo-it 736.000 memsize ( 0.000 retained)
11.000 objects ( 0.000 retained)
1.000 strings ( 0.000 retained)
memoist 192.000 memsize ( 0.000 retained)
4.000 objects ( 0.000 retained)
2.000 strings ( 0.000 retained)
Comparison:
memoist: 192 allocated
memery: 368 allocated - 1.92x more
memo-it: 736 allocated - 3.83x more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment