Class: Benchmark::Sweet::Comparison
- Inherits:
-
Object
- Object
- Benchmark::Sweet::Comparison
- Defined in:
- lib/benchmark/sweet/comparison.rb
Constant Summary collapse
- UNITS =
{"ips" => "i/s", "memsize" => "bytes", "memsize_retained" => "bytes"}.freeze
Instance Attribute Summary collapse
-
#baseline ⇒ Object
readonly
Returns the value of attribute baseline.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#metric ⇒ Object
readonly
Returns the value of attribute metric.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#worst ⇒ Object
readonly
Returns the value of attribute worst.
Instance Method Summary collapse
- #[](field) ⇒ Object
- #best? ⇒ Boolean
- #central_tendency ⇒ Object
- #color ⇒ Object
-
#comp_short(value = nil) ⇒ Object
I tend to call with: c.comp_short(“033[#cc.colorm#cc.central_tendencyc.central_tendency.round(1) #cc.unitse[0m”) # “033[31m#valuee[0m”.
-
#comp_string(l_to_s = nil) ⇒ Object
quick display.
- #diff_error ⇒ Object
- #error ⇒ Object
-
#initialize(metric, label, stats, offset, total, baseline, worst = nil) ⇒ Comparison
constructor
A new instance of Comparison.
- #mode ⇒ Object
-
#overlaps? ⇒ Boolean
True if it is basically the same as the best.
- #slowdown ⇒ Object
- #units ⇒ Object
- #worst? ⇒ Boolean
Constructor Details
#initialize(metric, label, stats, offset, total, baseline, worst = nil) ⇒ Comparison
Returns a new instance of Comparison.
7 8 9 10 11 12 13 14 15 |
# File 'lib/benchmark/sweet/comparison.rb', line 7 def initialize(metric, label, stats, offset, total, baseline, worst = nil) @metric = metric @label = label @stats = stats @offset = offset @total = total @baseline = baseline @worst = worst end |
Instance Attribute Details
#baseline ⇒ Object (readonly)
Returns the value of attribute baseline.
5 6 7 |
# File 'lib/benchmark/sweet/comparison.rb', line 5 def baseline @baseline end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
5 6 7 |
# File 'lib/benchmark/sweet/comparison.rb', line 5 def label @label end |
#metric ⇒ Object (readonly)
Returns the value of attribute metric.
5 6 7 |
# File 'lib/benchmark/sweet/comparison.rb', line 5 def metric @metric end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
6 7 8 |
# File 'lib/benchmark/sweet/comparison.rb', line 6 def offset @offset end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
5 6 7 |
# File 'lib/benchmark/sweet/comparison.rb', line 5 def stats @stats end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
6 7 8 |
# File 'lib/benchmark/sweet/comparison.rb', line 6 def total @total end |
#worst ⇒ Object (readonly)
Returns the value of attribute worst.
5 6 7 |
# File 'lib/benchmark/sweet/comparison.rb', line 5 def worst @worst end |
Instance Method Details
#[](field) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/benchmark/sweet/comparison.rb', line 17 def [](field) case field when :metric then metric when :comp_short then comp_short when :comp_string then comp_string when :label then label # not sure if this one makes sense else label[field] end end |
#best? ⇒ Boolean
35 |
# File 'lib/benchmark/sweet/comparison.rb', line 35 def best? ; !baseline || (baseline == stats) ; end |
#central_tendency ⇒ Object
27 |
# File 'lib/benchmark/sweet/comparison.rb', line 27 def central_tendency ; stats.central_tendency ; end |
#color ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/benchmark/sweet/comparison.rb', line 92 def color if !baseline ";0" elsif best? || overlaps? "32" elsif worst? "31" else ";0" end end |
#comp_short(value = nil) ⇒ Object
I tend to call with:
c.comp_short("\033[#{c.color}m#{c.central_tendency.round(1)} #{c.units}\e[0m") # "\033[31m#{value}\e[0m"
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/benchmark/sweet/comparison.rb', line 80 def comp_short(value = nil) value ||= "#{central_tendency.round(1)} #{units}" case mode when :best, :same value when :slower "%s - %.2fx (± %.2f)" % [value, slowdown, error] when :slowerish "%s - %.2fx" % [value, slowdown] end end |
#comp_string(l_to_s = nil) ⇒ Object
quick display
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/benchmark/sweet/comparison.rb', line 64 def comp_string(l_to_s = nil) l_to_s ||= -> l { l.to_s } case mode when :best "%20s: %10.1f %s" % [l_to_s.call(label), central_tendency, units] when :same "%20s: %10.1f %s - same-ish: difference falls within error" % [l_to_s.call(label), central_tendency, units] when :slower "%20s: %10.1f %s - %.2fx (± %.2f) slower" % [l_to_s.call(label), central_tendency, units, slowdown, error] when :slowerish "%20s: %10.1f %s - %.2fx slower" % [l_to_s.call(label), central_tendency, units, slowdown] end end |
#diff_error ⇒ Object
58 59 60 |
# File 'lib/benchmark/sweet/comparison.rb', line 58 def diff_error @diff_error ||= (slowdown ; @diff_error) end |
#error ⇒ Object
28 |
# File 'lib/benchmark/sweet/comparison.rb', line 28 def error ; stats.error ; end |
#mode ⇒ Object
31 32 33 |
# File 'lib/benchmark/sweet/comparison.rb', line 31 def mode @mode ||= best? ? :best : overlaps? ? :same : diff_error ? :slowerish : :slower end |
#overlaps? ⇒ Boolean
Returns true if it is basically the same as the best.
38 39 40 41 42 |
# File 'lib/benchmark/sweet/comparison.rb', line 38 def overlaps? return @overlaps if defined?(@overlaps) @overlaps = slowdown == 1 || stats && baseline && (stats.central_tendency == baseline.central_tendency || stats.overlaps?(baseline)) end |
#slowdown ⇒ Object
52 53 54 55 56 |
# File 'lib/benchmark/sweet/comparison.rb', line 52 def slowdown return @slowdown if @slowdown @slowdown, @diff_error = stats.slowdown(baseline) @slowdown end |
#units ⇒ Object
29 |
# File 'lib/benchmark/sweet/comparison.rb', line 29 def units ; UNITS[metric] || "objs" ; end |
#worst? ⇒ Boolean
44 45 46 47 48 49 50 |
# File 'lib/benchmark/sweet/comparison.rb', line 44 def worst? if @worst stats.overlaps?(@worst) else slowdown == Float::INFINITY || (total.to_i - 1 == offset.to_i && slowdown.to_i > 1) end end |