class Benchmark::Memory::Job::IOOutput::ComparisonFormatter

Format a comparison for use with the IOOutput.

Attributes

comparison[R]

@return [Report::Comparison] The comparison to format.

Public Class Methods

new(comparison) click to toggle source

Instantiate a formatter to output an comparison into an IO.

@param comparison [Report::Comparison] The comparison to format.

# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 15
def initialize(comparison)
  @comparison = comparison
end

Public Instance Methods

to_s() click to toggle source

Format comparison to a string to put on the output.

@return [String]

# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 25
def to_s
  return "" unless comparison.possible?

  output = StringIO.new
  best, *rest = comparison.entries
  rest = Array(rest)

  add_best_summary(best, output)

  rest.each do |entry|
    add_comparison(entry, best, output)
  end

  output.string
end

Private Instance Methods

add_best_summary(best, output) click to toggle source
# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 43
def add_best_summary(best, output)
  output << summary_message("%20s: %10i allocated\n", best)
end
add_comparison(entry, best, output) click to toggle source
# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 47
def add_comparison(entry, best, output)
  output << summary_message("%20s: %10i allocated - ", entry)
  output << comparison_between(entry, best)
  output << "\n"
end
comparison_between(entry, best) click to toggle source
# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 53
def comparison_between(entry, best)
  ratio = entry.allocated_memory.to_f / best.allocated_memory.to_f

  if ratio.abs > 1
    format("%.2fx more", ratio)
  else
    "same"
  end
end
summary_message(message, entry) click to toggle source
# File lib/benchmark/memory/job/io_output/comparison_formatter.rb, line 63
def summary_message(message, entry)
  format(message, entry.label, entry.allocated_memory)
end