Module: Benchmark::IPS::Helpers

Defined in:
lib/benchmark/ips.rb

Constant Summary collapse

SUFFIXES =
['', 'k', 'M', 'B', 'T', 'Q'].freeze

Class Method Summary collapse

Class Method Details

.humanize_duration(duration_ns) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/benchmark/ips.rb', line 126

def humanize_duration(duration_ns)
  if duration_ns < 1000
    "%.2f ns" % duration_ns
  elsif duration_ns < 1_000_000
    "%.2f μs" % (duration_ns / 1000)
  elsif duration_ns < 1_000_000_000
    "%.2f ms" % (duration_ns / 1_000_000)
  else
    "%.2f s" % (duration_ns / 1_000_000_000)
  end
end

.scale(value) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/benchmark/ips.rb', line 116

def scale(value)
  scale = (Math.log10(value) / 3).to_i
  scale = 0 if scale < 0 || scale >= SUFFIXES.size
  suffix = SUFFIXES[scale]
  scaled_value = value.to_f / (1000 ** scale)

  "%10.3f#{suffix}" % scaled_value
end