Skip to content

Instantly share code, notes, and snippets.

@davydovanton
Created July 9, 2016 08:00
Show Gist options
  • Save davydovanton/d5d1c888b5ce7bb3ca41a1bd49a67aa5 to your computer and use it in GitHub Desktop.
Save davydovanton/d5d1c888b5ce7bb3ca41a1bd49a67aa5 to your computer and use it in GitHub Desktop.
Benchmark: regex interpolation
require 'benchmark/ips'
GITHUB_COM = %r{https?://(?:www\.)?github\.com}i
Benchmark.ips do |x|
x.report('with o') { %r{\A#{GITHUB_COM}/([^/]+)/?\z}o }
x.report('without o') { %r{\A#{GITHUB_COM}/([^/]+)/?\z} }
x.compare!
end
# Warming up --------------------------------------
# with o 137.742k i/100ms
# without o 7.493k i/100ms
# Calculating -------------------------------------
# with o 10.680M (± 5.8%) i/s - 53.306M
# without o 80.892k (± 2.6%) i/s - 404.622k
#
# Comparison:
# with o: 10679946.9 i/s
# without o: 80891.8 i/s - 132.03x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment