Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store static regexps in constants for re-use #156

Merged
merged 2 commits into from
Oct 28, 2014

Conversation

pd
Copy link
Contributor

@pd pd commented Oct 27, 2014

Since they're static, there's no need to re-compile it every time.

Microbenchmark:

require 'benchmark'

STR = '2010-01-01T12:00:00Z'
RGXSTR = '^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:?\d\d)?)?$'
RGX = Regexp.new(RGXSTR)

N = 10_000

Benchmark.bmbm do |x|
  x.report('re-compiled') do
    N.times { Regexp.new(RGXSTR).match(STR) }
  end

  x.report('precompiled') do
    N.times { RGX.match(STR) }
  end
end

__END__
~ [chruby:2.1.1] » ruby bench.rb
Rehearsal -----------------------------------------------
re-compiled   0.770000   0.000000   0.770000 (  0.779001)
precompiled   0.010000   0.000000   0.010000 (  0.011617)
-------------------------------------- total: 0.780000sec

                  user     system      total        real
re-compiled   0.760000   0.000000   0.760000 (  0.766040)
precompiled   0.010000   0.000000   0.010000 (  0.008398)

I've got an app that is using draft3 + a lot of format: "date" and this made a decent dent in the runtime for tests validating the API.

@pd
Copy link
Contributor Author

pd commented Oct 28, 2014

@RST-J that seems like the right choice, yeah. Pushed a new commit to test that the anchoring is against string begin/end instead of line, and fixed the regexps.

@iainbeeston
Copy link
Contributor

Looks good to me. Nothing controversial

@RST-J
Copy link
Contributor

RST-J commented Oct 28, 2014

👍

1 similar comment
@hoxworth
Copy link
Contributor

👍

hoxworth added a commit that referenced this pull request Oct 28, 2014
Store static regexps in constants for re-use
@hoxworth hoxworth merged commit d00198c into voxpupuli:master Oct 28, 2014
@pd pd deleted the constant-regexps branch October 28, 2014 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants