diff --git a/lib/benchmark/ips.rb b/lib/benchmark/ips.rb index 706743e..01a0ba6 100644 --- a/lib/benchmark/ips.rb +++ b/lib/benchmark/ips.rb @@ -82,20 +82,15 @@ def self.options end module Helpers + SUFFIXES = ['', 'k', 'M', 'B', 'T', 'Q'].freeze + def scale(value) - scale = (Math.log10(value) / 3).to_i - suffix = case scale - when 1; 'k' - when 2; 'M' - when 3; 'B' - when 4; 'T' - when 5; 'Q' - else - # < 1000 or > 10^15, no scale or suffix - scale = 0 - ' ' - end - "%10.3f#{suffix}" % (value.to_f / (1000 ** scale)) + 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 module_function :scale end diff --git a/lib/benchmark/timing.rb b/lib/benchmark/timing.rb index 9563299..2e36205 100644 --- a/lib/benchmark/timing.rb +++ b/lib/benchmark/timing.rb @@ -1,5 +1,5 @@ module Benchmark - # Perform caclulations on Timing results. + # Perform calculations on Timing results. module Timing # Microseconds per second. MICROSECONDS_PER_SECOND = 1_000_000