Skip to content

match? should be faster than match #5256

Open
@ahorek

Description

@ahorek

Environment

jruby 9.2.1.0-SNAPSHOT (2.5.0) 2018-07-20 a2bc9a3 Java HotSpot(TM) 64-Bit Server VM 9.0.4+11 on 9.0.4+11 +jit [linux-x86_64]
ruby 2.6.0dev (2018-07-20 trunk 64004) [x86_64-linux]
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

require 'benchmark/ips'

pattern = /^([0-1]?[0-9]|2[0-3])([:\-,\.])?([0-5][0-9])$/
string = '9:30'

Benchmark.ips do |x|
  x.report('match') { string.match(pattern) }
  x.report('match?') { string.match?(pattern) }
end

ruby 2.5.0

               match    926.635k (± 2.6%) i/s -      4.634M in   5.004093s
              match?      3.395M (± 5.5%) i/s -     16.945M in   5.013444s

ruby master

               match      1.024M (± 4.4%) i/s -      5.123M in   5.013162s
              match?      4.809M (± 6.9%) i/s -     23.905M in   5.001929s

ruby master + jit

               match    984.690k (± 7.8%) i/s -      4.911M in   5.018228s
              match?      5.348M (±11.2%) i/s -     26.410M in   5.009641s

jruby master

               match      1.158M (± 9.2%) i/s -      5.712M in   4.995932s
              match?      1.222M (± 5.6%) i/s -      6.092M in   5.005920s

jruby master + indy

               match      1.211M (±11.0%) i/s -      5.912M in   4.985816s
              match?      1.362M (± 4.1%) i/s -      6.782M in   4.989386s

match? should perform better https://bugs.ruby-lang.org/issues/8110
but jruby implement it just like match, without setting a back-reference on a ruby level

ruby has a specialized method for it and it looks like there has been some improvements recently
https://github.com/ruby/ruby/blob/trunk/re.c#L3324

I found match? pattern very useful on many places, so I think it's worth investigating if there's a way to improve it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy