Last active
November 17, 2016 02:24
-
-
Save nateberkopec/63a17c92cf74e2204f45344dffe70bb1 to your computer and use it in GitHub Desktop.
Why is Regex.match faster with this string in Ruby 2.4?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'securerandom' | |
BIG_STRING = <<-EOS | |
{:event_id=>"c18ae35e83234021bb78d8ba8a8ef2da", :timestamp=>"2016-11-16T23:27:52", :time_spent=>nil, :level=>40, :platform=>"ruby", :sdk=>{"name"=>"sentry-raven", "version"=>"2.1.3"}, :contexts=>{:server_os=>{"name"=>"Darwin", "version"=>"Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64", "build"=>"16.1.0", "kernel_version"=>"Darwin nodachi.local 16.1.0 Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64"}, :runtime=>{"name"=>"ruby", "version"=>"ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-darwin16]"}}, :logger=>"", :culprit=>"benchmarks/benchmark.rb in / at line 10", :server_name=>"nodachi.local", :release=>"425c535", :environment=>"default", :modules=>{"rake"=>"11.3.0", "concurrent-ruby"=>"1.0.2", "i18n"=>"0.7.0", "minitest"=>"5.9.1", "thread_safe"=>"0.3.5", "tzinfo"=>"1.2.2", "activesupport"=>"5.0.0.1", "builder"=>"3.2.2", "erubis"=>"2.7.0", "mini_portile2"=>"2.1.0", "pkg-config"=>"1.1.7", "nokogiri"=>"1.6.8", "rails-dom-testing"=>"2.0.1", "loofah"=>"2.0.3", "rails-html-sanitizer"=>"1.0.3", "actionview"=>"5.0.0.1", "rack"=>"2.0.1", "rack-test"=>"0.6.3", "actionpack"=>"5.0.0.1", "ast"=>"2.3.0", "benchmark-ips"=>"2.5.0", "bundler"=>"1.13.6", "coderay"=>"1.1.1", "connection_pool"=>"2.2.0", "unicode_utils"=>"1.4.0", "coolline"=>"0.5.0", "diff-lcs"=>"1.2.5", "multipart-post"=>"2.0.0", "faraday"=>"0.9.2", "method_source"=>"0.8.2", "parser"=>"2.3.1.4", "power_assert"=>"0.3.1", "powerpack"=>"0.1.1", "slop"=>"3.6.0", "pry"=>"0.10.4", "pry-coolline"=>"0.2.5", "rack-protection"=>"1.5.3", "thor"=>"0.19.1", "railties"=>"5.0.0.1", "rainbow"=>"2.1.0", "redis"=>"3.3.1", "rspec-support"=>"3.5.0", "rspec-core"=>"3.5.4", "rspec-expectations"=>"3.5.0", "rspec-mocks"=>"3.5.0", "rspec"=>"3.5.0", "rspec-rails"=>"3.5.2", "ruby-progressbar"=>"1.8.1", "unicode-display_width"=>"1.1.1", "rubocop"=>"0.41.2", "sentry-raven"=>"2.1.3", "sidekiq"=>"4.2.2", "stackprof"=>"0.2.10", "test-unit"=>"3.2.1", "timecop"=>"0.8.1", "yajl-ruby"=>"1.3.0"}, :extra=>{}, :tags=>{}, :user=>{}, :logentry=>{:params=>nil, :message=>"ZeroDivisionError: divided by 0"}, :exception=>{:values=>[{:type=>"ZeroDivisionError", :value=>"divided by 0", :module=>"", :stacktrace=>{:frames=>[{:pre_context=>["\n", "LOGGER = Logger.new(nil)\n", "\n"], :post_context=>["\n", "Benchmark.ips do |x|\n", " x.config(:time => 5, :warmup => 2)\n"], :abs_path=>"benchmarks/benchmark.rb", :function=>"<main>", :lineno=>37, :in_app=>false, :context_line=>"puts Raven::Event.from_exception(build_exception).to_hash\n", :project_root=>nil, :filename=>"benchmarks/benchmark.rb"}, {:pre_context=>["#require_relative \"../spec/support/test_rails_app/app\"\n", "\n", "def build_exception\n"], :post_context=>["rescue ZeroDivisionError => exception\n", " exception\n", "end\n"], :abs_path=>"benchmarks/benchmark.rb", :function=>"build_exception", :lineno=>10, :in_app=>false, :context_line=>" 1 / 0\n", :project_root=>nil, :filename=>"benchmarks/benchmark.rb"}, {:pre_context=>["#require_relative \"../spec/support/test_rails_app/app\"\n", "\n", "def build_exception\n"], :post_context=>["rescue ZeroDivisionError => exception\n", " exception\n", "end\n"], :abs_path=>"benchmarks/benchmark.rb", :function=>"/", :lineno=>10, :in_app=>false, :context_line=>" 1 / 0\n", :project_root=>nil, :filename=>"benchmarks/benchmark.rb"}]}}]}, :message=>"ZeroDivisionError: divided by 0"} | |
EOS | |
CCREGEX = /^(?:\d[ -]*?){13,16}$/ | |
Benchmark.ips do |x| | |
x.config(:time => 5, :warmup => 2) | |
x.report("match") { CCREGEX.match(BIG_STRING) } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nodachi:raven-ruby nateberkopec$ ruby benchmarks/regex_benchmark.rb | |
Warming up -------------------------------------- | |
match 4.340k i/100ms | |
Calculating ------------------------------------- | |
match 44.996k (± 4.1%) i/s - 225.680k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby benchmarks/regex_benchmark.rb | |
Warming up -------------------------------------- | |
match 12.509k i/100ms | |
Calculating ------------------------------------- | |
match 130.241k (± 2.4%) i/s - 662.977k in 5.093459s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2.3.2
2.4.0-preview3