Skip to content

Instantly share code, notes, and snippets.

@zismailov
Created July 7, 2017 07:02
Show Gist options
  • Save zismailov/0feeed9a0d1be1589f939466dbeb7fc9 to your computer and use it in GitHub Desktop.
Save zismailov/0feeed9a0d1be1589f939466dbeb7fc9 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