Skip to content

Instantly share code, notes, and snippets.

@mkaschenko
Created November 18, 2014 05:08
Show Gist options
  • Select an option

  • Save mkaschenko/7b21bd968da775626cca to your computer and use it in GitHub Desktop.

Select an option

Save mkaschenko/7b21bd968da775626cca to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
@alphabet = Range.new('a', 'z').to_a.join(',')
def slow
@alphabet.gsub(',', '')
end
def fast
@alphabet.delete(',')
end
Benchmark.ips do |x|
x.report('slow') { slow }
x.report('fast') { fast }
x.compare!
end
Calculating -------------------------------------
slow 9.820k i/100ms
fast 65.006k i/100ms
-------------------------------------------------
slow 105.372k (± 4.2%) i/s - 530.280k
fast 1.068M (± 5.2%) i/s - 5.330M
Comparison:
fast: 1068256.8 i/s
slow: 105371.9 i/s - 10.14x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment