From 698f16dd7197582bbc9e16f017d50f3478d5c636 Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Sun, 3 Jan 2016 19:50:50 +0000 Subject: [PATCH] Add a benchmark for basic performance of memoised methods --- lib/memoist.rb | 1 + memoist.gemspec | 1 + script/benchmark.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 script/benchmark.rb diff --git a/lib/memoist.rb b/lib/memoist.rb index 62967dc..88b7ff2 100644 --- a/lib/memoist.rb +++ b/lib/memoist.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'memoist/version' require 'memoist/core_ext/singleton_class' module Memoist diff --git a/memoist.gemspec b/memoist.gemspec index 28a3e39..a8271da 100644 --- a/memoist.gemspec +++ b/memoist.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] + spec.add_development_dependency "benchmark-ips" spec.add_development_dependency "bundler" spec.add_development_dependency "rake" spec.add_development_dependency "minitest", "~> 5.5.1" diff --git a/script/benchmark.rb b/script/benchmark.rb new file mode 100644 index 0000000..ce5b41e --- /dev/null +++ b/script/benchmark.rb @@ -0,0 +1,48 @@ +$:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib") +require 'benchmark/ips' + +require 'memoist' + +class Benchy + extend Memoist + + def arity_0 + "Hello World" + end + memoize :arity_0 + + def arity_1(name) + "Hello #{name}" + end + memoize :arity_1 +end + +OBJECT = Benchy.new + +puts "Benchmarking: #{Memoist::VERSION}" + +Benchmark.ips do |x| + x.report("arity 0 - memoized") do |times| + times.times do + OBJECT.arity_0 + end + end + + # x.report("arity 0 - unmemoized") do |times| + # times.times do + # OBJECT._unmemoized_arity_0 + # end + # end + + x.report("arity 1 - memoized") do |times| + times.times do + OBJECT.arity_1(:World) + end + end + + # x.report("arity 1 - unmemoized") do |times| + # times.times do + # OBJECT._unmemoized_arity_1(:World) + # end + # end +end