Module: Runmetric
- Includes:
- Benchmark
- Defined in:
- lib/runmetric.rb
Overview
runmetric.rb
: a simple solution to hide the complexity of the Benchmark
module
Copyright © 2012 AJ AYIDJKIMO <[email protected]>
All rights reserved. You can redistribute and/or modify it under the same terms as Ruby.
Overview
This library provides the Runmetric
module, with its convenience method run_metric
that encapsulates (hides) some of the complexity of the Benchmark
module in the standard library. It was designed for use in online programming challenges, but it has application anytime performance (speed) information might be helpful or necessary in evaluating algorithm or application usage.
Usage
This example, from code written by the developer, illustrates how runmetric might be used:
def problem(max)
require 'runmetric'
include Runmetric
run_metric(iterations: 10**5) do
(max/2..max).inject(:lcm)
end
end
problem(20)
This output was automatically generated from the example code without use of puts
or inspect
methods:
Performance:
user system total real
0.312000 0.000000 0.312000 ( 0.321018)
Result(s):
232792560
Read the code in this module to understand how it operates and to see what other arguments the run_metric
method can take.
Instance Method Summary collapse
Instance Method Details
#run_metric(opts = {}, *any, &code) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/runmetric.rb', line 50 def run_metric(opts={}, *any, &code) iterations = opts[:iterations] || 1 reports = opts[:reports] || 1 if iterations == 1 then run_when = opts[:run_when] || :run_last_once else run_when = opts[:run_when] || :run_last end case run_when when :run_first # call code once to obtain results then within loop for timing run_code(*any, &code) time_code(reports, iterations, *any, &code) when :run_last # call code within loop for timing then once to obtain results time_code(reports, iterations, *any, &code) run_code(*any, &code) when :run_last_once # call code once to obtain results and timing time_and_run_code(*any, &code) else raise ArgumentError end end |