Created
March 18, 2018 10:02
-
-
Save ik5/173d94c004f1a4f9e3cb58fcc83aef1b to your computer and use it in GitHub Desktop.
Benchmark that calculate the difference between regexp and start_with?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
ik5
commented
Mar 18, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment