Sha256: baf1088cfa45f4c751dada6640f8544f1d61ad8f1e332ed94d354c3952fca786

Contents?: true

Size: 476 Bytes

Versions: 52

Compression:

Stored size: 476 Bytes

Contents

require 'benchmark'

TEST_AMOUNT = 2_000_000
Benchmark.bm(15) do |x|

  rgx = /\0\z/
  x.report('sub! + rgx') do
    TEST_AMOUNT.times do
      str = "hello\0"
      str.sub!(rgx, '')
    end
  end

  x.report('slice! + rgx') do
    TEST_AMOUNT.times do
      str = "hello\0"
      str.slice!(rgx)
    end
  end

  end_char = ?\0

  x.report('slice! + int + if') do
    TEST_AMOUNT.times do
      str = "hello\0"
      str.slice!(-1) if str[-1] == end_char
    end
  end

end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rack-app-11.0.2 spike/slice_vs_sub.rb
rack-app-11.0.1 spike/slice_vs_sub.rb
rack-app-11.0.0 spike/slice_vs_sub.rb
rack-app-10.0.0 spike/slice_vs_sub.rb
rack-app-9.0.0 spike/slice_vs_sub.rb
rack-app-8.1.0 spike/slice_vs_sub.rb
rack-app-8.0.1 spike/slice_vs_sub.rb
rack-app-8.0.0 spike/slice_vs_sub.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/spike/slice_vs_sub.rb
rack-app-7.7.0 spike/slice_vs_sub.rb
rack-app-7.6.5 spike/slice_vs_sub.rb
rack-app-7.6.4 spike/slice_vs_sub.rb
rack-app-7.6.3 spike/slice_vs_sub.rb
rack-app-7.6.2 spike/slice_vs_sub.rb
rack-app-7.6.1 spike/slice_vs_sub.rb
rack-app-7.6.0 spike/slice_vs_sub.rb
rack-app-7.5.2 spike/slice_vs_sub.rb
rack-app-7.5.1 spike/slice_vs_sub.rb
rack-app-7.5.0 spike/slice_vs_sub.rb
rack-app-7.4.0 spike/slice_vs_sub.rb