memoizable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +64 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/lib/memoizable.rb +17 -0
- data/memoizable.gemspec +56 -0
- data/spec/fibonacci_sample_spec.rb +26 -0
- data/spec/memoizable_spec.rb +61 -0
- data/spec/spec_helper.rb +9 -0
- metadata +77 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Enrique Comba Riepenhausen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= Memoizable
|
2
|
+
|
3
|
+
== Introduction
|
4
|
+
|
5
|
+
When doing recursion sometimes we are faced with the problem of performance
|
6
|
+
versus the beauty of the code we are implementing, specially when, due to
|
7
|
+
the recursive nature of the method we have written we seem to be computing
|
8
|
+
over and over the same.
|
9
|
+
|
10
|
+
The concept of memoization comes from the idea of capturing a certain
|
11
|
+
method call and saving it's result to an internal cache, so that, if this
|
12
|
+
particular method (with the same parameters) is called, it will return the
|
13
|
+
previously computed result.
|
14
|
+
|
15
|
+
I was inspired in writing this little module after reading an article
|
16
|
+
on James Edward Grey II'2 blog (http://blog.grayproductions.net/articles/caching_and_memoization)
|
17
|
+
|
18
|
+
== Example
|
19
|
+
|
20
|
+
Imagine we want to compute the Fibonacci sequence:
|
21
|
+
|
22
|
+
class Fibonacci
|
23
|
+
def fib(num)
|
24
|
+
return num if num < 2
|
25
|
+
fib(num -1) + fib(num - 2)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
As you can see immediately is that this will result in poor performance the
|
30
|
+
higher the number in the sequence we request as the algorithm will compute
|
31
|
+
over and over same method calls.
|
32
|
+
|
33
|
+
You could add some cache functionallity into your method and class, although
|
34
|
+
you would add new behaviour that the class and method shouldn't really have
|
35
|
+
as they should be dealing exclusively with the logic they are supposed to
|
36
|
+
execute; in this case the computation of the Fibonacci sequence.
|
37
|
+
|
38
|
+
Here is how the class would look like when we Memonize it:
|
39
|
+
|
40
|
+
class Fibonacci
|
41
|
+
include Memoizable
|
42
|
+
|
43
|
+
def fib(num)
|
44
|
+
return num if num < 2
|
45
|
+
fib(num -1) + fib(num - 2)
|
46
|
+
end
|
47
|
+
|
48
|
+
memoize :fib
|
49
|
+
end
|
50
|
+
|
51
|
+
== Note on Patches/Pull Requests
|
52
|
+
|
53
|
+
* Fork the project.
|
54
|
+
* Make your feature addition or bug fix.
|
55
|
+
* Add tests for it. This is important so I don't break it in a
|
56
|
+
future version unintentionally.
|
57
|
+
* Commit, do not mess with rakefile, version, or history.
|
58
|
+
(if you want to have your own version, that is fine but
|
59
|
+
bump version in a commit by itself I can ignore when I pull)
|
60
|
+
* Send me a pull request. Bonus points for topic branches.
|
61
|
+
|
62
|
+
== Copyright
|
63
|
+
|
64
|
+
Copyright (c) 2009 Enrique Comba Riepenhausen. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "memoizable"
|
8
|
+
gem.summary = %Q{Memoize method calls}
|
9
|
+
gem.description = %Q{Memoizes calls to method to boost the performance of recursive calls}
|
10
|
+
gem.email = "ecomba@nexwerk.com"
|
11
|
+
gem.homepage = "http://github.com/ecomba/memoizable"
|
12
|
+
gem.authors = ["Enrique Comba Riepenhausen"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
if File.exist?('VERSION')
|
39
|
+
version = File.read('VERSION')
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "memoizable #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/VERSION.yml
ADDED
data/lib/memoizable.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Memoizable
|
2
|
+
CACHE = Hash.new
|
3
|
+
module ClassMethods
|
4
|
+
def memoize(name)
|
5
|
+
original = "__original__#{name}"
|
6
|
+
alias_method original, name
|
7
|
+
define_method(name) do |*args|
|
8
|
+
CACHE[self.to_s.unpack("a*")<<name.to_s.unpack("a*")<<args] ||=
|
9
|
+
send(original, *args)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(receiver)
|
15
|
+
receiver.extend ClassMethods
|
16
|
+
end
|
17
|
+
end
|
data/memoizable.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{memoizable}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Enrique Comba Riepenhausen"]
|
12
|
+
s.date = %q{2009-12-21}
|
13
|
+
s.description = %q{Memoizes calls to method to boost the performance of recursive calls}
|
14
|
+
s.email = %q{ecomba@nexwerk.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION.yml",
|
26
|
+
"lib/memoizable.rb",
|
27
|
+
"memoizable.gemspec",
|
28
|
+
"spec/fibonacci_sample_spec.rb",
|
29
|
+
"spec/memoizable_spec.rb",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/ecomba/memoizable}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Memoize method calls}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/fibonacci_sample_spec.rb",
|
39
|
+
"spec/memoizable_spec.rb",
|
40
|
+
"spec/spec_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
describe "Fibonacci" do
|
6
|
+
class Fibonacci
|
7
|
+
def fib(number)
|
8
|
+
return number if number < 2
|
9
|
+
fib(number -1) + fib(number - 2)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "context" do
|
14
|
+
it "should run faster" do
|
15
|
+
fibo = Fibonacci.new
|
16
|
+
bm = Benchmark.measure { fibo.fib(30) }
|
17
|
+
class Fibonacci; include Memoizable; memoize :fib; end;
|
18
|
+
fibo2 = Fibonacci.new
|
19
|
+
bm2 = Benchmark.measure { fibo2.fib(30) }
|
20
|
+
|
21
|
+
# This test is flakey, I know... How to test the real speed?
|
22
|
+
bm.to_a[5].should > bm2.to_a[5]
|
23
|
+
bm2.to_a[5].should < 0.01
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Memoizable" do
|
4
|
+
before(:all) do
|
5
|
+
class Foo; def a; return "blaah" end; end;
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Module structure" do
|
9
|
+
it "should have a ClassMethods module" do
|
10
|
+
Memoizable::ClassMethods.class.should be(Module)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should contain a memonize method" do
|
14
|
+
Memoizable::ClassMethods.public_method_defined?(:memoize).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
context ": Cache" do
|
18
|
+
it "should be present" do
|
19
|
+
Memoizable::CACHE.should_not be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be a hash" do
|
23
|
+
Memoizable::CACHE.should be_instance_of(Hash)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "Class Inclusion" do
|
29
|
+
it "should extend a given class with the memoize method" do
|
30
|
+
class Foo; include Memoizable; end;
|
31
|
+
Foo.respond_to?(:memoize).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should alias the original method" do
|
35
|
+
Foo.memoize :a
|
36
|
+
foo = Foo.new
|
37
|
+
foo.respond_to?("__original__a").should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should modify the method" do
|
41
|
+
method_a = Foo.instance_method(:a)
|
42
|
+
Foo.memoize :a
|
43
|
+
method_a.should_not == Foo.instance_method(:a)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "Method calls" do
|
48
|
+
it "should return the same values" do
|
49
|
+
foo = Foo.new
|
50
|
+
first_return = foo.a
|
51
|
+
Foo.memoize :a
|
52
|
+
first_return.should == foo.a
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should put the method call into the cache" do
|
56
|
+
Foo.memoize :a
|
57
|
+
foo = Foo.new
|
58
|
+
Memoizable::CACHE.size.should > 0
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memoizable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Enrique Comba Riepenhausen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-21 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Memoizes calls to method to boost the performance of recursive calls
|
26
|
+
email: ecomba@nexwerk.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION.yml
|
41
|
+
- lib/memoizable.rb
|
42
|
+
- memoizable.gemspec
|
43
|
+
- spec/fibonacci_sample_spec.rb
|
44
|
+
- spec/memoizable_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/ecomba/memoizable
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Memoize method calls
|
74
|
+
test_files:
|
75
|
+
- spec/fibonacci_sample_spec.rb
|
76
|
+
- spec/memoizable_spec.rb
|
77
|
+
- spec/spec_helper.rb
|