Sha256: 418c9bcf514e6a95e6b7f8676d9f609f75b078a04713e3d2cc7524fe807ae07a

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# Run from the command line: bundle exec ruby benchmarks/format_string.rb
require_relative 'bm_setup'

display_benchmark_header

SINGLE_TOKEN_HASH = { greeting: 'Hello' }.freeze
MULTI_TOKEN_HASH = {
  greeting: 'Hello',
  name: 'Benchmark',
  message: 'Always a good idea to benchmark',
}.freeze

# rubocop:disable Style/FormatString
section "Format String" do |bench|
  bench.report("String#%") do
    '%10s' % 'hoge'
  end

  bench.report("format") do
    format '%10s', 'hoge'
  end

  bench.report("sprintf") do
    sprintf '%10s', 'hoge'
  end
end
# rubocop:enable Style/FormatString

__END__

### Environment

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
GC Disabled: false

### Test Cases

#### Format String

```
Warming up --------------------------------------
            String#%   148.922k i/100ms
              format   162.561k i/100ms
             sprintf   157.745k i/100ms
Calculating -------------------------------------
            String#%      2.363M (± 0.8%) i/s -     11.914M in   5.049141s
              format      2.668M (± 0.8%) i/s -     13.330M in   5.002105s
             sprintf      2.609M (± 0.8%) i/s -     13.093M in   5.025561s
                   with 95.0% confidence

Comparison:
              format:  2668054.9 i/s
             sprintf:  2609234.8 i/s - 1.02x  (± 0.01) slower
            String#%:  2363040.2 i/s - 1.13x  (± 0.01) slower
                   with 95.0% confidence
```

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
radius-spec-0.13.0 benchmarks/format_string.rb
radius-spec-0.12.1 benchmarks/format_string.rb
radius-spec-0.12.0 benchmarks/format_string.rb
radius-spec-0.11.0 benchmarks/format_string.rb
radius-spec-0.7.0 benchmarks/format_string.rb
radius-spec-0.6.0 benchmarks/format_string.rb
radius-spec-0.5.0 benchmarks/format_string.rb
radius-spec-0.4.0 benchmarks/format_string.rb
radius-spec-0.3.0 benchmarks/format_string.rb