From 418526610e230879efee983639b16b936de3aab5 Mon Sep 17 00:00:00 2001 From: Aslan Date: Wed, 13 Feb 2019 06:36:33 +0300 Subject: [PATCH] Did refactoring by template: require "benchmark/ips" def fast end def slow end Benchmark.ips do |x| x.report("fast code description") { fast } x.report("slow code description") { slow } x.compare! end --- code/array/shuffle-first-vs-sample.rb | 10 ++++----- code/enumerable/each-push-vs-map.rb | 10 ++++----- code/enumerable/each-vs-for-loop.rb | 10 ++++----- code/enumerable/map-flatten-vs-flat_map.rb | 10 ++++----- .../reverse-each-vs-reverse_each.rb | 10 ++++----- code/enumerable/select-first-vs-detect.rb | 10 ++++----- .../attr-accessor-vs-getter-and-setter.rb | 14 ++++++------ code/general/begin-rescue-vs-respond-to.rb | 14 ++++++------ code/hash/keys-each-vs-each_key.rb | 9 ++++---- code/hash/merge-bang-vs-[]=.rb | 10 ++++----- code/hash/merge-vs-merge-bang.rb | 10 ++++----- code/proc-and-block/block-vs-to_proc.rb | 10 ++++----- code/proc-and-block/proc-call-vs-yield.rb | 10 ++++----- code/string/casecmp-vs-downcase-==.rb | 10 ++++----- code/string/concatenation.rb | 22 +++++++++---------- .../end-string-checking-match-vs-end_with.rb | 10 ++++----- code/string/gsub-vs-sub.rb | 12 +++++----- code/string/gsub-vs-tr.rb | 10 ++++----- .../remove-extra-spaces-or-other-chars.rb | 10 ++++----- ...art-string-checking-match-vs-start_with.rb | 10 ++++----- code/string/sub-vs-chomp-vs-delete_suffix.rb | 12 +++++----- 21 files changed, 116 insertions(+), 117 deletions(-) diff --git a/code/array/shuffle-first-vs-sample.rb b/code/array/shuffle-first-vs-sample.rb index 49a639b..374e1b4 100644 --- a/code/array/shuffle-first-vs-sample.rb +++ b/code/array/shuffle-first-vs-sample.rb @@ -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 diff --git a/code/enumerable/each-push-vs-map.rb b/code/enumerable/each-push-vs-map.rb index 78ed9fa..e5a6f54 100644 --- a/code/enumerable/each-push-vs-map.rb +++ b/code/enumerable/each-push-vs-map.rb @@ -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 diff --git a/code/enumerable/each-vs-for-loop.rb b/code/enumerable/each-vs-for-loop.rb index 2b7c1ce..7fd611b 100644 --- a/code/enumerable/each-vs-for-loop.rb +++ b/code/enumerable/each-vs-for-loop.rb @@ -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 diff --git a/code/enumerable/map-flatten-vs-flat_map.rb b/code/enumerable/map-flatten-vs-flat_map.rb index 25e0e79..a55e1dd 100644 --- a/code/enumerable/map-flatten-vs-flat_map.rb +++ b/code/enumerable/map-flatten-vs-flat_map.rb @@ -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 @@ -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 diff --git a/code/enumerable/reverse-each-vs-reverse_each.rb b/code/enumerable/reverse-each-vs-reverse_each.rb index 2ddf6a8..f1f15ae 100644 --- a/code/enumerable/reverse-each-vs-reverse_each.rb +++ b/code/enumerable/reverse-each-vs-reverse_each.rb @@ -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 diff --git a/code/enumerable/select-first-vs-detect.rb b/code/enumerable/select-first-vs-detect.rb index 6b7f0bf..fd1c03a 100644 --- a/code/enumerable/select-first-vs-detect.rb +++ b/code/enumerable/select-first-vs-detect.rb @@ -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 \ No newline at end of file diff --git a/code/general/attr-accessor-vs-getter-and-setter.rb b/code/general/attr-accessor-vs-getter-and-setter.rb index f6c442b..1c659e8 100644 --- a/code/general/attr-accessor-vs-getter-and-setter.rb +++ b/code/general/attr-accessor-vs-getter-and-setter.rb @@ -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 diff --git a/code/general/begin-rescue-vs-respond-to.rb b/code/general/begin-rescue-vs-respond-to.rb index ba3bb89..7751b65 100644 --- a/code/general/begin-rescue-vs-respond-to.rb +++ b/code/general/begin-rescue-vs-respond-to.rb @@ -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 diff --git a/code/hash/keys-each-vs-each_key.rb b/code/hash/keys-each-vs-each_key.rb index fd3dd6a..4817607 100644 --- a/code/hash/keys-each-vs-each_key.rb +++ b/code/hash/keys-each-vs-each_key.rb @@ -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 diff --git a/code/hash/merge-bang-vs-[]=.rb b/code/hash/merge-bang-vs-[]=.rb index 16b099f..67d3df4 100644 --- a/code/hash/merge-bang-vs-[]=.rb +++ b/code/hash/merge-bang-vs-[]=.rb @@ -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 diff --git a/code/hash/merge-vs-merge-bang.rb b/code/hash/merge-vs-merge-bang.rb index df501d5..99602b7 100644 --- a/code/hash/merge-vs-merge-bang.rb +++ b/code/hash/merge-vs-merge-bang.rb @@ -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 diff --git a/code/proc-and-block/block-vs-to_proc.rb b/code/proc-and-block/block-vs-to_proc.rb index 10845e1..531fe31 100644 --- a/code/proc-and-block/block-vs-to_proc.rb +++ b/code/proc-and-block/block-vs-to_proc.rb @@ -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 diff --git a/code/proc-and-block/proc-call-vs-yield.rb b/code/proc-and-block/proc-call-vs-yield.rb index 3e95968..457bf06 100644 --- a/code/proc-and-block/proc-call-vs-yield.rb +++ b/code/proc-and-block/proc-call-vs-yield.rb @@ -1,5 +1,9 @@ require 'benchmark/ips' +def fast + yield +end + def slow(&block) block.call end @@ -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 diff --git a/code/string/casecmp-vs-downcase-==.rb b/code/string/casecmp-vs-downcase-==.rb index 027f748..bcb40b1 100644 --- a/code/string/casecmp-vs-downcase-==.rb +++ b/code/string/casecmp-vs-downcase-==.rb @@ -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 diff --git a/code/string/concatenation.rb b/code/string/concatenation.rb index e249a30..0817316 100644 --- a/code/string/concatenation.rb +++ b/code/string/concatenation.rb @@ -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' @@ -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 diff --git a/code/string/end-string-checking-match-vs-end_with.rb b/code/string/end-string-checking-match-vs-end_with.rb index e95d87a..34f6fce 100644 --- a/code/string/end-string-checking-match-vs-end_with.rb +++ b/code/string/end-string-checking-match-vs-end_with.rb @@ -2,6 +2,10 @@ SLUG = "some_kind_of_root_url" +def fast + SLUG.end_with?('_path', '_url') +end + def slower SLUG =~ /_(path|url)$/ end @@ -10,13 +14,9 @@ def slow SLUG.match?(/_(path|url)$/) end -def fast - SLUG.end_with?('_path', '_url') -end - Benchmark.ips do |x| + x.report('String#end_with?') { fast } x.report('String#=~') { slower } x.report('String#match?') { slow } if RUBY_VERSION >= "2.4.0".freeze - x.report('String#end_with?') { fast } x.compare! end diff --git a/code/string/gsub-vs-sub.rb b/code/string/gsub-vs-sub.rb index 1d0804d..874d19e 100644 --- a/code/string/gsub-vs-sub.rb +++ b/code/string/gsub-vs-sub.rb @@ -2,10 +2,6 @@ URL = 'http://www.thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/wearejustdoingthistobestupidnowsincethiscangoonforeverandeverandeverbutitstilllookskindaneatinthebrowsereventhoughitsabigwasteoftimeandenergyandhasnorealpointbutwehadtodoitanyways.html' -def slow - URL.gsub('http://', 'https://') -end - def fast URL.sub('http://', 'https://') end @@ -16,9 +12,13 @@ def fastest str end +def slow + URL.gsub('http://', 'https://') +end + Benchmark.ips do |x| - x.report('String#gsub') { slow } - x.report('String#sub') { fast } x.report('String#dup["string"]=') { fastest } + x.report('String#sub') { fast } + x.report('String#gsub') { slow } x.compare! end diff --git a/code/string/gsub-vs-tr.rb b/code/string/gsub-vs-tr.rb index 6edbeee..aaa97ee 100644 --- a/code/string/gsub-vs-tr.rb +++ b/code/string/gsub-vs-tr.rb @@ -2,16 +2,16 @@ SLUG = 'writing-fast-ruby' -def slow - SLUG.gsub('-', ' ') -end - def fast SLUG.tr('-', ' ') end +def slow + SLUG.gsub('-', ' ') +end + Benchmark.ips do |x| - x.report('String#gsub') { slow } x.report('String#tr') { fast } + x.report('String#gsub') { slow } x.compare! end diff --git a/code/string/remove-extra-spaces-or-other-chars.rb b/code/string/remove-extra-spaces-or-other-chars.rb index fb1349f..5ba6bcb 100644 --- a/code/string/remove-extra-spaces-or-other-chars.rb +++ b/code/string/remove-extra-spaces-or-other-chars.rb @@ -9,16 +9,16 @@ raise unless PASSAGE.gsub(/ +/, " ") == PASSAGE.squeeze(" ") -def slow - PASSAGE.gsub(/ +/, " ") -end - def fast PASSAGE.squeeze(" ") end +def slow + PASSAGE.gsub(/ +/, " ") +end + Benchmark.ips do |x| - x.report('String#gsub/regex+/') { slow } x.report('String#squeeze') { fast } + x.report('String#gsub/regex+/') { slow } x.compare! end diff --git a/code/string/start-string-checking-match-vs-start_with.rb b/code/string/start-string-checking-match-vs-start_with.rb index a58eb35..f61d582 100644 --- a/code/string/start-string-checking-match-vs-start_with.rb +++ b/code/string/start-string-checking-match-vs-start_with.rb @@ -2,6 +2,10 @@ SLUG = 'test_some_kind_of_long_file_name.rb' +def fast + SLUG.start_with?('test_') +end + def slower SLUG =~ /^test_/ end @@ -10,13 +14,9 @@ def slow SLUG.match?(/^test_/) end -def fast - SLUG.start_with?('test_') -end - Benchmark.ips do |x| + x.report('String#start_with?') { fast } x.report('String#=~') { slower } x.report('String#match?') { slow } if RUBY_VERSION >= "2.4.0".freeze - x.report('String#start_with?') { fast } x.compare! end diff --git a/code/string/sub-vs-chomp-vs-delete_suffix.rb b/code/string/sub-vs-chomp-vs-delete_suffix.rb index 6ecfcf9..2135273 100644 --- a/code/string/sub-vs-chomp-vs-delete_suffix.rb +++ b/code/string/sub-vs-chomp-vs-delete_suffix.rb @@ -2,10 +2,6 @@ SLUG = 'YourSubclassType' -def slow - SLUG.sub(/Type\z/, '') -end - def fast SLUG.chomp('Type') end @@ -14,9 +10,13 @@ def faster SLUG.delete_suffix('Type') end +def slow + SLUG.sub(/Type\z/, '') +end + Benchmark.ips do |x| - x.report('String#sub') { slow } - x.report("String#chomp") { fast } x.report("String#delete_suffix") { faster } if RUBY_VERSION >= '2.5.0' + x.report("String#chomp") { fast } + x.report('String#sub') { slow } x.compare! end