Skip to content

Instantly share code, notes, and snippets.

@myokoym
Last active February 15, 2018 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myokoym/0b075cb0b89465a374f292f0b283d983 to your computer and use it in GitHub Desktop.
Save myokoym/0b075cb0b89465a374f292f0b283d983 to your computer and use it in GitHub Desktop.
[観察日記 2018-02-14 - なるせにっき](http://naruse.hateblo.jp/entry/2018/02/14/221859) 「配列の全ての要素が等しいか否か」のベンチマークスクリプトと結果
% ruby -v benchmark-same-all.rb
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
["a","a",...] user system total real
all? 0.490852 0.000000 0.490852 ( 0.492231)
uniq.size 0.249663 0.039904 0.289567 ( 0.290653)
lazy cnt 0.387460 0.000005 0.387465 ( 0.388847)
lazy t2cnt 0.409444 0.000000 0.409444 ( 0.410681)
each_cons 1.134791 0.000000 1.134791 ( 1.137745)
lazy one? 0.394611 0.000000 0.394611 ( 0.395936)
rotate 0.023589 0.011946 0.035535 ( 0.035795)
range 0.006502 0.000000 0.006502 ( 0.006506)
map all? 2.558119 0.023979 2.582098 ( 2.587691)
map uniq.size 2.380210 0.104611 2.484821 ( 2.491208)
map lazy cnt 3.187991 0.011623 3.199614 ( 3.221779)
map lazy t2cnt 3.043720 0.000000 3.043720 ( 3.047949)
map each_cons 2.801011 0.019936 2.820947 ( 2.821781)
map lazy one? 2.918080 0.000000 2.918080 ( 2.918291)
map rotate 1.434895 0.031964 1.466859 ( 1.467404)
map range 1.574584 0.036031 1.610615 ( 1.610860)
["b","a",...] user system total real
all? 0.000009 0.000000 0.000009 ( 0.000005)
uniq.size 0.524196 0.032005 0.556201 ( 0.556199)
lazy cnt 0.390134 0.000000 0.390134 ( 0.390147)
lazy t2cnt 0.000030 0.000000 0.000030 ( 0.000029)
each_cons 0.000007 0.000000 0.000007 ( 0.000006)
lazy one? 0.000008 0.000000 0.000008 ( 0.000007)
rotate 0.390551 0.011957 0.402508 ( 0.402512)
range 0.000005 0.000000 0.000005 ( 0.000004)
map all? 0.000006 0.000000 0.000006 ( 0.000006)
map uniq.size 1.914109 0.056030 1.970139 ( 1.970635)
map lazy cnt 3.144191 0.004001 3.148192 ( 3.148900)
map lazy t2cnt 0.000040 0.000000 0.000040 ( 0.000039)
map each_cons 1.471266 0.000000 1.471266 ( 1.471515)
map lazy one? 0.000019 0.000000 0.000019 ( 0.000019)
map rotate 1.408608 0.023948 1.432556 ( 1.432553)
map range 1.762782 0.000037 1.762819 ( 1.763363)
#!/usr/bin/env ruby
require "benchmark"
def run(n, element)
ary = Array.new(n) { element }
[element, element.next].each do |first_element|
ary[0] = first_element
title = "[#{ary[0].inspect},#{ary[1].inspect},...]"
caption = title + Benchmark::CAPTION.sub(/\A\s+/, " ")
Benchmark.benchmark(caption) do |x|
x.report("all? ") do
ary.all? {|e| e == ary[0] }
end
x.report("uniq.size ") do
ary.uniq.size == 1
end
x.report("lazy cnt ") do
ary.lazy.uniq.count == 1
end
x.report("lazy t2cnt") do
ary.lazy.uniq.take(2).count == 1
end
x.report("each_cons ") do
ary.each_cons(2).all? { |a,b| a == b }
end
x.report("lazy one? ") do
ary.lazy.uniq.one?
end
x.report("rotate ") do
ary.rotate == ary
end
x.report("range ") do
ary[0..-2] == ary[1..-1]
end
x.report("map all? ") do
ary.all? {|e| e.next == ary[0].next }
end
x.report("map uniq.size ") do
ary.map(&:next).uniq.size == 1
end
x.report("map lazy cnt ") do
ary.lazy.map(&:next).uniq.count == 1
end
x.report("map lazy t2cnt") do
ary.lazy.map(&:next).uniq.take(2).count == 1
end
x.report("map each_cons ") do
ary.map(&:next).each_cons(2).all? { |a,b| a == b }
end
x.report("map lazy one? ") do
ary.lazy.map(&:next).uniq.one?
end
x.report("map rotate ") do
mapped_ary = ary.map(&:next)
mapped_ary.rotate == mapped_ary
end
x.report("map range ") do
mapped_ary = ary.map(&:next)
mapped_ary[0..-2] == mapped_ary[1..-1]
end
end
end
end
run(10 ** 7, "a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment