Skip to content

Instantly share code, notes, and snippets.

@dskecse
Created August 15, 2016 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dskecse/d929d5fa1274eaa70e636f19c8ca74c1 to your computer and use it in GitHub Desktop.
Save dskecse/d929d5fa1274eaa70e636f19c8ca74c1 to your computer and use it in GitHub Desktop.
ruby 2.3.1
require 'benchmark'
n = 5_000_000
Benchmark.bmbm do |x|
x.report('String#match') do
n.times do
'metadata:foo=bar'.match(/metadata:/)
end
end
x.report('String#=~') do
n.times do
'metadata:foo=bar' =~ /metadata:/
end
end
x.report('String#start_with?') do
n.times do
'metadata:foo=bar'.start_with? 'metadata:'
end
end
end
# Rehearsal ------------------------------------------------------
# String#match 5.010000 0.010000 5.020000 ( 5.031097)
# String#=~ 1.460000 0.000000 1.460000 ( 1.461489)
# String#start_with? 0.680000 0.000000 0.680000 ( 0.694000)
# --------------------------------------------- total: 7.160000sec
# user system total real
# String#match 4.960000 0.010000 4.970000 ( 4.975006)
# String#=~ 1.420000 0.000000 1.420000 ( 1.423937)
# String#start_with? 0.640000 0.000000 0.640000 ( 0.641189)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment