clarkware / ruby-learning-tests

Unit tests I wrote way back when I started learning Ruby.

This URL has Read+Write access

ruby-learning-tests / benchmark.rb
100755 25 lines (17 sloc) 0.334 kb
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
#!/usr/bin/env ruby
 
require "benchmark"
 
Benchmark.bm(label_width = 10) do |test|
 
  loops = 50000
 
  test.report("for:") do
    y = 0
    (1..loops).each { |x| y += x }
  end
 
  test.report("times:") do
    y = 0
    loops.times { |x| y += x }
  end
 
  test.report("upto:") do
    y = 0
    1.upto(loops) { |x| y += x }
  end
 
end