Skip to content

Commit 06c9a00

Browse files
authored
Merge pull request #164 from danarnold/master
Add Regexp#match? and Regexp#match to regex matching benchmark
2 parents 9a0d928 + 9ce5e97 commit 06c9a00

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ Comparison:
12541254
String#[0...n] ==: 427206.8 i/s - 4.79x slower
12551255
```
12561256

1257-
##### `Regexp#===` vs `String#match` vs `String#=~` vs `String#match?` [code ](code/string/===-vs-=~-vs-match.rb)
1257+
##### `Regexp#===` vs `Regexp#match` vs `Regexp#match?` vs `String#match` vs `String#=~` vs `String#match?` [code ](code/string/===-vs-=~-vs-match.rb)
12581258

12591259
`String#match?` and `Regexp#match?` are available on Ruby 2.4 or later.
12601260
ActiveSupport [provides](http://guides.rubyonrails.org/v5.1/active_support_core_extensions.html#match-questionmark)
@@ -1270,19 +1270,22 @@ improvement.
12701270
12711271
```
12721272
$ ruby -v code/string/===-vs-=~-vs-match.rb
1273-
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin17]
1274-
1275-
Calculating -------------------------------------
1276-
String#match? 6.284M (± 5.6%) i/s - 31.324M in 5.001471s
1277-
String#=~ 2.581M (± 4.7%) i/s - 12.977M in 5.038887s
1278-
Regexp#=== 2.482M (± 4.1%) i/s - 12.397M in 5.002808s
1279-
String#match 2.097M (± 4.3%) i/s - 10.592M in 5.060535s
1280-
1281-
Comparison:
1282-
String#match?: 6283591.8 i/s
1283-
String#=~: 2581356.8 i/s - 2.43x slower
1284-
Regexp#===: 2482379.7 i/s - 2.53x slower
1285-
String#match: 2096984.3 i/s - 3.00x slower
1273+
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
1274+
Calculating -------------------------------------
1275+
Regexp#match? 6.994M (± 3.0%) i/s - 35.144M in 5.029647s
1276+
String#match? 6.909M (± 3.3%) i/s - 34.663M in 5.023177s
1277+
String#=~ 2.784M (± 5.2%) i/s - 13.996M in 5.043168s
1278+
Regexp#=== 2.702M (± 4.5%) i/s - 13.631M in 5.056215s
1279+
Regexp#match 2.607M (± 4.9%) i/s - 13.025M in 5.009071s
1280+
String#match 2.362M (± 5.7%) i/s - 11.817M in 5.020344s
1281+
1282+
Comparison:
1283+
Regexp#match?: 6994107.7 i/s
1284+
String#match?: 6909055.7 i/s - same-ish: difference falls within error
1285+
String#=~: 2783577.8 i/s - 2.51x slower
1286+
Regexp#===: 2702030.0 i/s - 2.59x slower
1287+
Regexp#match: 2607484.0 i/s - 2.68x slower
1288+
String#match: 2362314.8 i/s - 2.96x slower
12861289
```
12871290

12881291
See [#59](https://github.com/JuanitoFatas/fast-ruby/pull/59) and [#62](https://github.com/JuanitoFatas/fast-ruby/pull/62) for discussions.

code/string/===-vs-=~-vs-match.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
require "benchmark/ips"
22

3+
def fastest
4+
/boo/.match?('foo'.freeze)
5+
end
6+
37
def fast
48
"foo".freeze.match?(/boo/)
59
end
@@ -12,14 +16,20 @@ def slower
1216
/boo/ === "foo".freeze
1317
end
1418

19+
def even_slower
20+
/boo/.match('foo'.freeze)
21+
end
22+
1523
def slowest
1624
"foo".freeze.match(/boo/)
1725
end
1826

1927
Benchmark.ips do |x|
28+
x.report("Regexp#match?") { fastest } if RUBY_VERSION >= "2.4.0".freeze
2029
x.report("String#match?") { fast } if RUBY_VERSION >= "2.4.0".freeze
2130
x.report("String#=~") { slow }
2231
x.report("Regexp#===") { slower }
32+
x.report("Regexp#match") { even_slower }
2333
x.report("String#match") { slowest }
2434
x.compare!
2535
end

0 commit comments

Comments
 (0)