Skip to content

Instantly share code, notes, and snippets.

@ccocchi
Last active May 10, 2019 10:07
Show Gist options
  • Save ccocchi/3b95beb0a95563ce16a764c375d841d6 to your computer and use it in GitHub Desktop.
Save ccocchi/3b95beb0a95563ce16a764c375d841d6 to your computer and use it in GitHub Desktop.
Regexp VS start_with?
require 'benchmark/ips'
require 'uri'
str = 'https://www.facebook.com/'
Benchmark.ips do |x|
x.report('start_with?') { str.start_with?('https://', 'http://') }
x.report('match?') { str.match?(URI::regexp) }
x.report('=~') { str =~ URI::regexp }
x.compare!
end
@ccocchi
Copy link
Author

ccocchi commented May 10, 2019

Warming up --------------------------------------
         start_with?   210.748k i/100ms
              match?    65.575k i/100ms
                  =~    59.756k i/100ms
Calculating -------------------------------------
         start_with?      6.592M (± 9.8%) i/s -     32.666M in   5.018034s
              match?    879.694k (±10.1%) i/s -      4.394M in   5.060208s
                  =~    803.385k (±14.1%) i/s -      3.944M in   5.064482s

Comparison:
         start_with?:  6591721.7 i/s
              match?:   879694.5 i/s - 7.49x  slower
                  =~:   803384.5 i/s - 8.20x  slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment