Skip to content

Instantly share code, notes, and snippets.

@seth
Forked from myronmarston/results
Created October 3, 2010 15:36
Show Gist options
  • Save seth/608668 to your computer and use it in GitHub Desktop.
Save seth/608668 to your computer and use it in GitHub Desktop.
$ ./start_with_and_index.rb
user system total real
index fail 0.420000 0.000000 0.420000 ( 0.424444)
start_with? fail 0.990000 0.000000 0.990000 ( 0.993577)
index pass 0.420000 0.000000 0.420000 ( 0.424240)
start_with? pass 1.650000 0.020000 1.670000 ( 1.672174)
#!/usr/bin/env ruby
require 'benchmark'
times = 1000000
string = "this is a string"
Benchmark.bm(16) do |x|
x.report("index fail") do
times.times do
string.index("this is a stripe")
end
end
x.report("start_with? fail") do
times.times do
string.start_with?("this is a stripe")
end
end
####
x.report("index pass") do
times.times do
string.index("this is a string")
end
end
x.report("start_with? pass") do
times.times do
string.start_with?("this is a string")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment