fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

Add Regexp#=== to match and =~ comparison #62

Closed JuanitoFatas closed 8 years ago

JuanitoFatas commented 9 years ago

Follow up of https://github.com/JuanitoFatas/fast-ruby/pull/59

On my machine String#=~ is faster than Regexp#===.

What do you guys think?

/cc @Arcovion @schneems

https://github.com/schneems/rails/commit/1bf50badd943e684a56a03392ef0ddafefca0ad7#commitcomment-12572839 https://github.com/schneems/rails/commit/752432e82f3032e96ba609b65437cd8e8b8d3939

schneems commented 9 years ago

Confirm

Calculating -------------------------------------
           String#=~    67.365k i/100ms
          Regexp#===    63.076k i/100ms
        String#match    56.745k i/100ms
-------------------------------------------------
           String#=~      1.645M (±12.9%) i/s -      8.084M
          Regexp#===      1.511M (±12.1%) i/s -      7.443M
        String#match      1.208M (±15.2%) i/s -      5.901M

Weird, I could have sworn I saw it faster earlier

Arcovion commented 9 years ago

Hmm, yea very little in it. They do different things too, with Regexp#=== returning a boolean (which is handy, as before I knew that I used !!str[/match/]). I think it's good to include, to show that they are equally fast.

schneems commented 9 years ago

String#=~ is (1.645 - 1.511 ) / 1.511 #=> 8 % faster

Arcovion commented 9 years ago

Same benchmark:

Calculating -------------------------------------
           String#=~   138.285k i/100ms
          Regexp#===   130.622k i/100ms
        String#match   112.077k i/100ms
-------------------------------------------------
           String#=~      3.273M (± 2.6%) i/s -     16.456M
          Regexp#===      3.406M (± 4.3%) i/s -     16.981M
        String#match      2.714M (± 2.8%) i/s -     13.561M

Comparison:
          Regexp#===:  3406168.4 i/s
           String#=~:  3273161.4 i/s - 1.04x slower
        String#match:  2714176.6 i/s - 1.25x slower
ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]

Mine has the lowest standard deviation @ ± 4.3%, also higher IPS Essentially equal for me.

Edit: Ran it again, got similar to https://github.com/JuanitoFatas/fast-ruby/pull/59#issuecomment-128514614 but higher IPS:

Calculating -------------------------------------
           String#=~   143.101k i/100ms
          Regexp#===   138.872k i/100ms
        String#match   118.070k i/100ms
-------------------------------------------------
           String#=~      3.332M (± 1.9%) i/s -     16.743M
          Regexp#===      3.482M (± 1.5%) i/s -     17.498M
        String#match      2.746M (± 1.2%) i/s -     13.814M

Comparison:
          Regexp#===:  3481922.4 i/s
           String#=~:  3332209.4 i/s - 1.04x slower
        String#match:  2745894.1 i/s - 1.27x slower
JuanitoFatas commented 8 years ago

Merged in https://github.com/JuanitoFatas/fast-ruby/commit/69c1ed1ab4ef5a6c64b5ad59c84167e6194c9b0c. Thanks everyone.

SamSaffron commented 6 years ago

Got to add "test".match?(/e/) as well cause that is fastest.