runmetric 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/runmetric.rb +124 -0
- metadata +49 -0
data/lib/runmetric.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
=begin rdoc
|
2
|
+
|
3
|
+
== +runmetric.rb+: a simple solution to hide the complexity of the +Benchmark+ module
|
4
|
+
|
5
|
+
Copyright (c) 2012 AJ AYIDJKIMO <aj.presenttense@gmail.com>
|
6
|
+
|
7
|
+
All rights reserved. You can redistribute and/or modify it under the same terms as Ruby.
|
8
|
+
|
9
|
+
== Overview
|
10
|
+
|
11
|
+
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.
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
This example, from code written by the developer, illustrates how runmetric might be used:
|
16
|
+
|
17
|
+
def problem(max)
|
18
|
+
|
19
|
+
require 'runmetric'
|
20
|
+
include Runmetric
|
21
|
+
|
22
|
+
run_metric(iterations: 10**5) do
|
23
|
+
(max/2..max).inject(:lcm)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
problem(20)
|
29
|
+
|
30
|
+
This output was automatically generated from the example code without use of +puts+ or +inspect+ methods:
|
31
|
+
|
32
|
+
Performance:
|
33
|
+
|
34
|
+
user system total real
|
35
|
+
0.312000 0.000000 0.312000 ( 0.321018)
|
36
|
+
|
37
|
+
Result(s):
|
38
|
+
|
39
|
+
232792560
|
40
|
+
|
41
|
+
Read the code in this module to understand how it operates and to see what other arguments the +run_metric+ method can take.
|
42
|
+
|
43
|
+
=end
|
44
|
+
|
45
|
+
module Runmetric
|
46
|
+
|
47
|
+
require 'benchmark'
|
48
|
+
include Benchmark
|
49
|
+
|
50
|
+
def run_metric(opts={}, *any, &code)
|
51
|
+
|
52
|
+
iterations = opts[:iterations] || 1
|
53
|
+
reports = opts[:reports] || 1
|
54
|
+
if iterations == 1 then
|
55
|
+
run_when = opts[:run_when] || :run_last_once
|
56
|
+
else
|
57
|
+
run_when = opts[:run_when] || :run_last
|
58
|
+
end
|
59
|
+
|
60
|
+
case run_when
|
61
|
+
when :run_first
|
62
|
+
|
63
|
+
# call code once to obtain results then within loop for timing
|
64
|
+
run_code(*any, &code)
|
65
|
+
time_code(reports, iterations, *any, &code)
|
66
|
+
|
67
|
+
when :run_last
|
68
|
+
|
69
|
+
# call code within loop for timing then once to obtain results
|
70
|
+
time_code(reports, iterations, *any, &code)
|
71
|
+
run_code(*any, &code)
|
72
|
+
|
73
|
+
when :run_last_once
|
74
|
+
|
75
|
+
# call code once to obtain results and timing
|
76
|
+
time_and_run_code(*any, &code)
|
77
|
+
|
78
|
+
else
|
79
|
+
raise ArgumentError
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def run_code(*any, &code) #:doc:
|
87
|
+
|
88
|
+
puts "\nResult(s):\n\n"
|
89
|
+
puts code.call(*any)
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def time_and_run_code(*any, &code) #:doc:
|
94
|
+
|
95
|
+
results = 0
|
96
|
+
|
97
|
+
puts "\nPerformance:\n\n"
|
98
|
+
bm do |x|
|
99
|
+
x.report do
|
100
|
+
results = code.call(*any)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
puts "\nResult(s):\n\n"
|
105
|
+
puts results
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
def time_code(reports, iterations, *any, &code) #:doc:
|
110
|
+
|
111
|
+
puts "\nPerformance:\n\n"
|
112
|
+
reports.times do
|
113
|
+
bm do |x|
|
114
|
+
x.report do
|
115
|
+
iterations.times do
|
116
|
+
code.call(*any)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: runmetric
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- AJ Ayidjkimo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This library provides the Runmetric module, with its convenience method
|
15
|
+
run_metric that encapsulates (hides) some of the complexity of the Benchmark module
|
16
|
+
in the standard library. It was designed for use in online programming challenges,
|
17
|
+
but it has application anytime performance (speed) information might be helpful
|
18
|
+
or necessary in evaluating algorithm or application usage.
|
19
|
+
email: aj.presenttense@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- lib/runmetric.rb
|
25
|
+
homepage: http://www.technopolissoftware.com
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '1.9'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.24
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Simple solution to hide the complexity of the Benchmark module
|
49
|
+
test_files: []
|