Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring by template #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions code/array/shuffle-first-vs-sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

ARRAY = [*1..100]

def slow
ARRAY.shuffle.first
end

def fast
ARRAY.sample
end

def slow
ARRAY.shuffle.first
end

Benchmark.ips do |x|
x.report('Array#shuffle.first') { slow }
x.report('Array#sample') { fast }
x.report('Array#shuffle.first') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/enumerable/each-push-vs-map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

ARRAY = (1..100).to_a

def fast
ARRAY.map { |i| i }
end

def slow
array = []
ARRAY.each { |i| array.push i }
end

def fast
ARRAY.map { |i| i }
end

Benchmark.ips do |x|
x.report('Array#each + push') { slow }
x.report('Array#map') { fast }
x.report('Array#each + push') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/enumerable/each-vs-for-loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

ARRAY = [*1..100]

def slow
for number in ARRAY do
def fast
ARRAY.each do |number|
number
end
end

def fast
ARRAY.each do |number|
def slow
for number in ARRAY do
number
end
end

Benchmark.ips do |x|
x.report('For loop') { slow }
x.report('#each') { fast }
x.report('For loop') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/enumerable/map-flatten-vs-flat_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

ARRAY = (1..100).to_a

def fast
ARRAY.flat_map { |e| [e, e] }
end

def slow_flatten_1
ARRAY.map { |e| [e, e] }.flatten(1)
end
Expand All @@ -10,13 +14,9 @@ def slow_flatten
ARRAY.map { |e| [e, e] }.flatten
end

def fast
ARRAY.flat_map { |e| [e, e] }
end

Benchmark.ips do |x|
x.report('Array#flat_map') { fast }
x.report('Array#map.flatten(1)') { slow_flatten_1 }
x.report('Array#map.flatten') { slow_flatten }
x.report('Array#flat_map') { fast }
x.compare!
end
10 changes: 5 additions & 5 deletions code/enumerable/reverse-each-vs-reverse_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

ARRAY = (1..100).to_a

def slow
ARRAY.reverse.each{|x| x}
end

def fast
ARRAY.reverse_each{|x| x}
end

def slow
ARRAY.reverse.each{|x| x}
end

Benchmark.ips do |x|
x.report('Array#reverse.each') { slow }
x.report('Array#reverse_each') { fast }
x.report('Array#reverse.each') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/enumerable/select-first-vs-detect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

ARRAY = [*1..100]

def slow
ARRAY.select { |x| x.eql?(15) }.first
end

def fast
ARRAY.detect { |x| x.eql?(15) }
end

def slow
ARRAY.select { |x| x.eql?(15) }.first
end

Benchmark.ips(20) do |x|
x.report('Enumerable#select.first') { slow }
x.report('Enumerable#detect') { fast }
x.report('Enumerable#select.first') { slow }
x.compare!
end
14 changes: 7 additions & 7 deletions code/general/attr-accessor-vs-getter-and-setter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ def last_name=(value)

end

def slow
user = User.new
user.last_name = 'John'
user.last_name
end

def fast
user = User.new
user.first_name = 'John'
user.first_name
end

def slow
user = User.new
user.last_name = 'John'
user.last_name
end

Benchmark.ips do |x|
x.report('getter_and_setter') { slow }
x.report('attr_accessor') { fast }
x.report('getter_and_setter') { slow }
x.compare!
end
14 changes: 7 additions & 7 deletions code/general/begin-rescue-vs-respond-to.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'benchmark/ips'

def slow
begin
def fast
if respond_to?(:writing)
writing
rescue
else
'fast ruby'
end
end

def fast
if respond_to?(:writing)
def slow
begin
writing
else
rescue
'fast ruby'
end
end

Benchmark.ips do |x|
x.report('begin...rescue') { slow }
x.report('respond_to?') { fast }
x.report('begin...rescue') { slow }
x.compare!
end
9 changes: 4 additions & 5 deletions code/hash/keys-each-vs-each_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@
}
}

def fast
HASH.each_key(&:to_sym)
end

def slow
HASH.keys.each(&:to_sym)
end

def fast
HASH.each_key(&:to_sym)
end

Benchmark.ips do |x|
x.report('Hash#keys.each') { slow }
x.report('Hash#each_key') { fast }
x.report('Hash#keys.each') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/hash/merge-bang-vs-[]=.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

ENUM = (1..100)

def slow
def fast
ENUM.each_with_object({}) do |e, h|
h.merge!(e => e)
h[e] = e
end
end

def fast
def slow
ENUM.each_with_object({}) do |e, h|
h[e] = e
h.merge!(e => e)
end
end

Benchmark.ips do |x|
x.report('Hash#merge!') { slow }
x.report('Hash#[]=') { fast }
x.report('Hash#merge!') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/hash/merge-vs-merge-bang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

ENUM = (1..100)

def slow
def fast
ENUM.inject({}) do |h, e|
h.merge(e => e)
h.merge!(e => e)
end
end

def fast
def slow
ENUM.inject({}) do |h, e|
h.merge!(e => e)
h.merge(e => e)
end
end

Benchmark.ips do |x|
x.report('Hash#merge') { slow }
x.report('Hash#merge!') { fast }
x.report('Hash#merge') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/proc-and-block/block-vs-to_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

RANGE = (1..100)

def slow
RANGE.map { |i| i.to_s }
end

def fast
RANGE.map(&:to_s)
end

def slow
RANGE.map { |i| i.to_s }
end

Benchmark.ips do |x|
x.report('Block') { slow }
x.report('Symbol#to_proc') { fast }
x.report('Block') { slow }
x.compare!
end
10 changes: 5 additions & 5 deletions code/proc-and-block/proc-call-vs-yield.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'benchmark/ips'

def fast
yield
end

def slow(&block)
block.call
end
Expand All @@ -12,14 +16,10 @@ def slow3(&block)

end

def fast
yield
end

Benchmark.ips do |x|
x.report('yield') { fast { 1 + 1 } }
x.report('block.call') { slow { 1 + 1 } }
x.report('block + yield') { slow2 { 1 + 1 } }
x.report('unused block') { slow3 { 1 + 1 } }
x.report('yield') { fast { 1 + 1 } }
x.compare!
end
10 changes: 5 additions & 5 deletions code/string/casecmp-vs-downcase-==.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

SLUG = 'ABCD'

def slow
SLUG.downcase == 'abcd'
end

def fast
SLUG.casecmp('abcd') == 0
end

def slow
SLUG.downcase == 'abcd'
end

Benchmark.ips do |x|
x.report('String#downcase + ==') { slow }
x.report('String#casecmp') { fast }
x.report('String#downcase + ==') { slow }
x.compare!
end
22 changes: 11 additions & 11 deletions code/string/concatenation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
require 'benchmark/ips'

# 1 object
def fast
'foo' 'bar'
end

def fast_interpolation
"#{'foo'}#{'bar'}"
end

# 2 + 1 = 3 object
def slow_plus
'foo' + 'bar'
Expand All @@ -15,20 +24,11 @@ def slow_append
'foo' << 'bar'
end

# 1 object
def fast
'foo' 'bar'
end

def fast_interpolation
"#{'foo'}#{'bar'}"
end

Benchmark.ips do |x|
x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
x.report('"foo" "bar"') { fast }
x.report('String#+') { slow_plus }
x.report('String#concat') { slow_concat }
x.report('String#append') { slow_append }
x.report('"foo" "bar"') { fast }
x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
x.compare!
end
Loading