Skip to content

Instantly share code, notes, and snippets.

@leejarvis

leejarvis/meh.rb Secret

Created January 13, 2016 16:43
Show Gist options
  • Save leejarvis/9f8c7b96a32e019a3cf8 to your computer and use it in GitHub Desktop.
Save leejarvis/9f8c7b96a32e019a3cf8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark/ips'
CALLER = caller
REGEX = /(\/app\/(controllers|models|views)\/.+)/.freeze
def constant_regex
CALLER.each { |x| x.match(REGEX) }
end
def literal_regex
CALLER.each { |x| x.match(/(\/app\/(controllers|models|views)\/.+)/) }
end
def squiggly
CALLER.each { |x| x =~ REGEX }
end
Benchmark.ips do |x|
x.report("constant") { constant_regex }
x.report("literal") { literal_regex }
x.report("squiggly") { squiggly }
x.compare!
end
__END__
Calculating -------------------------------------
constant 136.776k i/100ms
literal 139.374k i/100ms
squiggly 138.983k i/100ms
-------------------------------------------------
constant 6.643M (± 5.6%) i/s - 33.237M
literal 6.529M (± 7.3%) i/s - 32.474M
squiggly 6.502M (± 5.6%) i/s - 32.383M
Comparison:
constant: 6642507.7 i/s
literal: 6529384.0 i/s - 1.02x slower
squiggly: 6501559.2 i/s - 1.02x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment