File: bench_up_downto_times.rb

package info (click to toggle)
jruby 1.5.6-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 45,088 kB
  • ctags: 77,093
  • sloc: ruby: 398,491; java: 170,202; yacc: 3,782; xml: 2,529; sh: 299; tcl: 40; makefile: 35; ansic: 23
file content (69 lines) | stat: -rw-r--r-- 1,572 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'benchmark'

COUNT = 10_000_000
COUNT_STR = "10M"

def bench_upto(bm)
  puts "Integer#upto -----------------"
  bm.report("#{COUNT_STR} Integer#upto, arg block") do
    1.upto(COUNT) { |i| i }
  end

  bm.report("#{COUNT_STR} Integer#upto, arg block and add") do
    1.upto(COUNT) { |i| i + 1 }
  end

  bm.report("#{COUNT_STR} Integer#upto, no-arg block") do
    1.upto(COUNT) { 0 }
  end

  bm.report("#{COUNT_STR} Integer#upto, no-arg block and add") do
    i= 0
    1.upto(COUNT) { i += 1 }
  end

  puts "Integer#downto -----------------"
  bm.report("#{COUNT_STR} Integer#downto, arg block") do
    COUNT.downto(1) { |i| i }
  end

  bm.report("#{COUNT_STR} Integer#downto, arg block and add") do
    COUNT.downto(1) { |i| i + 1 }
  end

  bm.report("#{COUNT_STR} Integer#downto, no-arg block") do
    COUNT.downto(1) { 0 }
  end

  bm.report("#{COUNT_STR} Integer#downto, no-arg block and add") do
    i= 0
    COUNT.downto(1) { i += 1 }
  end

  puts "Integer#times -----------------"
  bm.report("#{COUNT_STR} Integer#times, arg block") do
    COUNT.times { |i| i }
  end

  bm.report("#{COUNT_STR} Integer#times, arg block and add") do
    COUNT.times { |i| i + 1 }
  end

  bm.report("#{COUNT_STR} Integer#times, no-arg block") do
    COUNT.times { 0 }
  end

  bm.report("#{COUNT_STR} Integer#times, no-arg block and add") do
    i= 0
    COUNT.times { i += 1 }
  end
end

if $0 == __FILE__
  (ARGV[0] || 5).to_i.times { |iter|
    puts "============= Iteration #{iter + 1} ============= "
    Benchmark.bm(40) {|bm|
      bench_upto(bm)
    }
  }
end