Skip to content

Instantly share code, notes, and snippets.

@rsiddle
Last active March 5, 2018 21:32
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 rsiddle/ec3f48d00c12619b5d25304365190f78 to your computer and use it in GitHub Desktop.
Save rsiddle/ec3f48d00c12619b5d25304365190f78 to your computer and use it in GitHub Desktop.
require 'benchmark'
Benchmark.bm(7) do |x|
STR = 'l2yanS'
REGEX1 = /^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6}$/
REGEX2 = /^(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6}$/
x.report("Using backslash d:") { 1_000_000.times do
STR =~ REGEX1
end }
x.report("Using 0-9:") { 1_000_000.times do
STR =~ REGEX2
end }
end
# RESULTS
user system total real
Using backslash d: 0.450000 0.010000 0.460000 ( 0.453032)
Using 0-9: 0.470000 0.000000 0.470000 ( 0.475317)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment