Skip to content

Commit

Permalink
reduces calc overhead of suffix helper method (#128)
Browse files Browse the repository at this point in the history
whitespace commits don't count

s/caclulations/calculations
  • Loading branch information
jtbg authored Mar 31, 2023
1 parent ea96ad5 commit 3316f6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions lib/benchmark/ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/benchmark/timing.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3316f6d

Please sign in to comment.