Skip to content

Commit 895b4a8

Browse files
committed
add substring inclusion checking
1 parent 9591cbb commit 895b4a8

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -985,25 +985,30 @@ Comparison:
985985
> This is only useful for cases where you are checking <br>
986986
> for a match and not using the resultant match object. <br>
987987
> :warning: <br>
988-
> `Regexp#===` is also faster than `String#match` but you need to switch the order of arguments.
988+
> `Regexp#===` is also faster than `String#match` but you need to switch the order of arguments.<br>
989+
> :warning: <br>
990+
> `String#include?` This is only useful for cases where you are checking substring inclusion
989991
990992
```
991-
$ ruby -v code/string/===-vs-=~-vs-match.rb.rb
992-
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
993+
$ ruby -v code/string/===-vs-=~-vs-match.rb
994+
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
993995
996+
Warming up --------------------------------------
997+
String#include? 238.465k i/100ms
998+
String#=~ 161.826k i/100ms
999+
Regexp#=== 147.986k i/100ms
1000+
String#match 136.749k i/100ms
9941001
Calculating -------------------------------------
995-
String#=~ 98.184k i/100ms
996-
Regexp#=== 92.382k i/100ms
997-
String#match 83.601k i/100ms
998-
-------------------------------------------------
999-
String#=~ 2.442M (± 7.6%) i/s - 12.175M
1000-
Regexp#=== 2.259M (± 7.9%) i/s - 11.271M
1001-
String#match 1.840M (± 7.3%) i/s - 9.196M
1002+
String#include? 5.961M (± 8.5%) i/s - 29.808M in 5.035615s
1003+
String#=~ 2.843M (± 8.1%) i/s - 14.241M in 5.040590s
1004+
Regexp#=== 2.689M (± 7.3%) i/s - 13.467M in 5.032779s
1005+
String#match 2.371M (± 5.2%) i/s - 11.897M in 5.031192s
10021006
10031007
Comparison:
1004-
String#=~: 2442335.1 i/s
1005-
Regexp#===: 2259277.3 i/s - 1.08x slower
1006-
String#match: 1839815.4 i/s - 1.33x slower
1008+
String#include?: 5960785.4 i/s
1009+
String#=~: 2843384.2 i/s - 2.10x slower
1010+
Regexp#===: 2689290.6 i/s - 2.22x slower
1011+
String#match: 2370791.0 i/s - 2.51x slower
10071012
```
10081013

10091014
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
require "benchmark/ips"
22

33
def fastest
4+
"foo".freeze.include? 'boo'
5+
end
6+
7+
def faster
48
"foo".freeze =~ /boo/
59
end
610

@@ -13,7 +17,8 @@ def slow
1317
end
1418

1519
Benchmark.ips do |x|
16-
x.report("String#=~") { fastest }
20+
x.report("String#include?") { fastest }
21+
x.report("String#=~") { faster }
1722
x.report("Regexp#===") { fast }
1823
x.report("String#match") { slow }
1924
x.compare!

0 commit comments

Comments
 (0)