new_bench.rb

Innokenty Mikhailov, 05/16/2012 05:15 PM

Download (957 Bytes)

 
1
require 'benchmark'
2
N = 1000
3
DATA_LENGTH = 1000
4
DATA = [1] * DATA_LENGTH
5

    
6
def bencher(lazy)
7
  Benchmark.bm(15) do |r|
8
    r.report(ARGV[0]) do
9
      N.times { lazy.each { |x| x * 100 } }
10
    end
11
  end
12
end
13

    
14
def puts_header(name)
15
  puts "======================Lazy##{name}"
16
end
17

    
18
puts_header("map")
19
bencher(DATA.lazy.map { |x| x * 9 }.map { |x| x * 55 })
20
puts_header("select")
21
bencher(DATA.lazy.select { |x| x > 0 }.select { |x| x > 0 })
22
puts_header("grep")
23
bencher(DATA.lazy.grep(proc { |x| x > 0 }).grep(proc { |x| x > 0 }))
24
puts_header("take_while")
25
bencher(DATA.lazy.take_while { |x| x > 0 }.take_while { |x| x > 0 })
26
puts_header("drop_while")
27
bencher(DATA.lazy.drop_while { |x| x < 0 }.drop_while { |x| x < 0 })
28
puts_header("reject")
29
bencher(DATA.lazy.reject { |x| x < 0 }.reject { |x| x < 0 })
30
puts_header("drop")
31
bencher((DATA * 4).lazy.drop(DATA_LENGTH * 2).drop(DATA_LENGTH))
32
puts_header("take")
33
bencher(DATA.lazy.take(DATA_LENGTH).take(DATA_LENGTH))