This repository has been archived by the owner on May 21, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ lib/flyweight.rb | |
test/test_helper.rb | ||
test/test_flyweight.rb | ||
example/example.rb | ||
example/benchmark.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/local/bin/ruby -w | ||
|
||
$VERBOSE = true | ||
|
||
require 'benchmark' | ||
require_relative '../lib/flyweight' | ||
|
||
class Material | ||
include Flyweight | ||
|
||
def initialize(looks, price, other=nil) | ||
@looks, @price, @other = looks, price, other | ||
end | ||
end | ||
|
||
class HeavyParts | ||
DATA = 'str' * 10000000 | ||
|
||
def initialize | ||
@content = DATA.dup | ||
end | ||
end | ||
|
||
TIMES = 100000 | ||
|
||
Benchmark.bm do |bm| | ||
bm.report 'Light Instant .new' do | ||
TIMES.times do | ||
Material.new :metal, 2000 | ||
end | ||
end | ||
|
||
bm.report 'Light Instant .intern' do | ||
TIMES.times do | ||
Material.intern :metal, 2000 | ||
end | ||
end | ||
|
||
bm.report 'Light Keep .new' do | ||
TIMES.times.map do | ||
Material.new :metal, 2000 | ||
end | ||
end | ||
|
||
bm.report 'Light Keep .intern' do | ||
TIMES.times.map do | ||
Material.intern :metal, 2000 | ||
end | ||
end | ||
|
||
bm.report 'Heavy Instant .new' do | ||
TIMES.times do | ||
Material.new :metal, 2000, HeavyParts.new | ||
end | ||
end | ||
|
||
bm.report 'Heavy Instant .intern' do | ||
TIMES.times do | ||
Material.intern :metal, 2000, HeavyParts.new | ||
end | ||
end | ||
|
||
bm.report 'Heavy Keep .new' do | ||
TIMES.times.map do | ||
Material.new :metal, 2000, HeavyParts.new | ||
end | ||
end | ||
|
||
bm.report 'Heavy Keep .intern' do | ||
TIMES.times.map do | ||
Material.intern :metal, 2000, HeavyParts.new | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters