Skip to content

Instantly share code, notes, and snippets.

@ik5
Created March 18, 2018 10:02
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 ik5/173d94c004f1a4f9e3cb58fcc83aef1b to your computer and use it in GitHub Desktop.
Save ik5/173d94c004f1a4f9e3cb58fcc83aef1b to your computer and use it in GitHub Desktop.
Benchmark that calculate the difference between regexp and start_with?
require 'benchmark'
require 'securerandom'
N = 1_000_000
REGEX = /^(abc|Abc|ABC)/
def gen_randstr(len = 24)
SecureRandom.base64(len)
end
Benchmark.bm(13) do |x|
x.report('start_with?') do
N.times do
gen_randstr.start_with?('abc', 'Abc', 'ABC')
end
end
x.report('regex') do
N.times do
gen_randstr =~ REGEX
end
end
end
@ik5
Copy link
Author

ik5 commented Mar 18, 2018

                    user     system      total        real
start_with?     0.879497   0.480250   1.359747 (  1.363121)
regex           0.854619   0.472788   1.327407 (  1.331010)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment