Skip to content

Instantly share code, notes, and snippets.

@raszi
Created July 7, 2015 17:58
Show Gist options
  • Save raszi/de8e9efa7f26c6ae729d to your computer and use it in GitHub Desktop.
Save raszi/de8e9efa7f26c6ae729d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark/ips'
Benchmark.ips do |x|
input = 's' * 1_000_000
root = '/something/funny/is/here/'
root_filter = /^#{Regexp.escape(root)}/io
root_filter_a = /\A#{Regexp.escape(root)}/io
x.report(:start_with?) { !input.downcase.start_with?(root) }
x.report(:match_optimized) { !(input =~ root_filter) }
x.report(:match_optimized_A) { !(input =~ root_filter_a) }
x.report(:match_original) { !(input =~ /^#{Regexp.escape(root)}/i) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment