Skip to content

Instantly share code, notes, and snippets.

@pocari
Created July 28, 2014 15:10
Show Gist options
  • Save pocari/b1102ba618f011b60cac to your computer and use it in GitHub Desktop.
Save pocari/b1102ba618f011b60cac to your computer and use it in GitHub Desktop.
ary = [5, 4, 1, 7, 3, 8]
#案1
p ary.each_with_index.select{|e, i| e >= 5}.map{|e| e[1]} #=>[0, 3, 5]
#案2
p ary.each_with_index.each_with_object([]){|(e, i), acc| acc << i if e >= 5} #=>[0, 3, 5]
#coding: windows-31J
require 'benchmark'
def calc_time(comment)
val = nil
t = Benchmark.realtime {
val = yield
}
puts "#{comment} #{t} (msec)"
val
end
size = 10000000
ary = size.times.map{rand(size)}
calc_time("#案1") {
ary.each_with_index.select{|e, i| e >= 5}.map{|e| e[1]} #=>[0, 3, 5]
}
calc_time("#案2") {
ary.each_with_index.each_with_object([]){|(e, i), acc| acc << i if e >= 5} #=>[0, 3, 5]
}
calc_time("#i18nさん") {
ary.map.with_index { |e,i| e >= 5 ? i : nil }.compact
}
calc_time("#nabetaniさん1") {
ary.size.times.select{ |x| 5<=ary[x] }
}
calc_time("#cielavenirさん") {
ary.each_with_index.lazy.select{|e,i|e>=5}.map(&:last).force
}
calc_time("#cielavenirさん2") {
ary.flat_map.with_index{|e,i|e>=5 ? i : []}
}
calc_time("#nabetaniさん2") {
r=[]
b = ary.size
loop{
if b=ary[0,b].rindex{ |x| 5<=x } then r << b else break r.reverse; end
}
}
#ruby 2.0.0p247 (2013-06-27) [x64-mingw32]
#案1 3.219184 (msec)
#案2 2.044117 (msec)
#i18nさん 1.114063 (msec)
#nabetaniさん1 1.139066 (msec)
#cielavenirさん 7.587434 (msec)
#cielavenirさん2 1.712097 (msec)
#nabetaniさん2 3.168182 (msec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment