public
Description: Statistically correct benchmarking for Ruby.
Homepage:
Clone URL: git://github.com/Pistos/better-benchmark.git
Pistos (author)
Wed Feb 11 06:41:19 -0800 2009
commit  d59a8aabce08c604ec1b58308a34002a25c048af
tree    5a85c4cf0a927bb5f67c57f3ebf5f8616ae8acb3
parent  10bbdf6981c7bd752d17260e3ae76d7a81a38d9f
better-benchmark / example.rb
100644 29 lines (23 sloc) 0.772 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
26
27
28
29
#!/usr/bin/env ruby
 
require 'rubygems'
require 'better-benchmark'
 
# Provide two blocks of code to compare. For example, two blocks that
# accomplish the same thing, but differ in implementation. For optimal
# results, the amount of time to execute a single iteration should be large
# enough to adequately diminish the significance of any startup and stoppage
# time of one iteration. The number of benchmark iterations should not be
# too large; better to increase the amount of work done per iteration.
 
result = Benchmark.compare_realtime(
  :iterations => 20,
  :inner_iterations => 500_000,
  :verbose => true
) { |iteration|
  if 1 < 2
    x = 'foo'
  else
    x = 'bar'
  end
}.with { |iteration|
  x = ( 1 < 2 ? 'foo' : 'bar' )
}
 
Benchmark.report_on result