Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created October 3, 2010 04:03
Show Gist options
  • Save myronmarston/608250 to your computer and use it in GitHub Desktop.
Save myronmarston/608250 to your computer and use it in GitHub Desktop.
$ ruby start_with_benchmark.rb
user system total real
start_with? fail 0.520000 0.000000 0.520000 ( 0.519693)
=~ fail 0.220000 0.000000 0.220000 ( 0.220458)
start_with? pass 0.880000 0.000000 0.880000 ( 0.887475)
=~ pass 0.610000 0.000000 0.610000 ( 0.608715)
require 'benchmark'
times = 1000000
string = "this is a string"
Benchmark.bm(16) do |x|
x.report("start_with? fail") do
times.times do
string.start_with?("thit")
end
end
x.report("=~ fail") do
times.times do
string =~ /^thit/
end
end
x.report("start_with? pass") do
times.times do
string.start_with?("this")
end
end
x.report("=~ pass") do
times.times do
string =~ /^this/
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment