Skip to content

Commit e951321

Browse files
committed
Add String#match? to String#start_with?/String#end_with? comparison
1 parent 9907db5 commit e951321

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

README.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,11 @@ Comparison:
975975
String#+: 2977282.7 i/s - 1.80x slower
976976
```
977977

978-
##### `String#match` vs `String#start_with?`/`String#end_with?` [code (start)](code/string/start-string-checking-match-vs-start_with.rb) [code (end)](code/string/end-string-checking-match-vs-end_with.rb)
978+
##### `String#match` vs `String.match?` vs `String#start_with?`/`String#end_with?` [code (start)](code/string/start-string-checking-match-vs-start_with.rb) [code (end)](code/string/end-string-checking-match-vs-end_with.rb)
979+
980+
The regular expression approaches become slower as the tested string becomes
981+
longer. For short strings, `String#match?` performs similarly to
982+
`String#start_with?`/`String#end_with?`.
979983

980984
> :warning: <br>
981985
> Sometimes you cant replace regexp with `start_with?`, <br>
@@ -988,34 +992,32 @@ Comparison:
988992
989993
```
990994
$ ruby -v code/string/start-string-checking-match-vs-start_with.rb
991-
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
992-
993-
Calculating -------------------------------------
994-
String#=~ 56.672k i/100ms
995-
String#start_with? 118.308k i/100ms
996-
-------------------------------------------------
997-
String#=~ 919.574k (± 6.4%) i/s - 4.590M
998-
String#start_with? 4.177M (± 6.4%) i/s - 20.822M
995+
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin17]
999996
1000-
Comparison:
1001-
String#start_with?: 4177162.6 i/s
1002-
String#=~: 919574.2 i/s - 4.54x slower
997+
Calculating -------------------------------------
998+
String#=~ 1.088M (± 4.0%) i/s - 5.471M in 5.034404s
999+
String#match? 5.138M (± 5.0%) i/s - 25.669M in 5.008810s
1000+
String#start_with? 6.314M (± 4.3%) i/s - 31.554M in 5.007207s
1001+
1002+
Comparison:
1003+
String#start_with?: 6314182.0 i/s
1004+
String#match?: 5138115.1 i/s - 1.23x slower
1005+
String#=~: 1088461.5 i/s - 5.80x slower
10031006
```
10041007

10051008
```
10061009
$ ruby -v code/string/end-string-checking-match-vs-end_with.rb
1007-
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
1008-
1009-
Calculating -------------------------------------
1010-
String#=~ 53.194k i/100ms
1011-
String#end_with? 105.871k i/100ms
1012-
-------------------------------------------------
1013-
String#=~ 891.124k (± 7.2%) i/s - 4.468M
1014-
String#end_with? 2.942M (± 7.6%) i/s - 14.610M
1015-
1016-
Comparison:
1017-
String#end_with?: 2942017.4 i/s
1018-
String#=~: 891124.1 i/s - 3.30x slower
1010+
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin17]
1011+
1012+
Calculating -------------------------------------
1013+
String#=~ 918.101k (± 6.0%) i/s - 4.650M in 5.084079s
1014+
String#match? 3.009M (± 6.8%) i/s - 14.991M in 5.005691s
1015+
String#end_with? 4.548M (± 9.3%) i/s - 22.684M in 5.034115s
1016+
1017+
Comparison:
1018+
String#end_with?: 4547871.0 i/s
1019+
String#match?: 3008554.5 i/s - 1.51x slower
1020+
String#=~: 918100.5 i/s - 4.95x slower
10191021
```
10201022

10211023
##### `String#start_with?` vs `String#[].==` [code](code/string/start_with-vs-substring-==.rb)
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
require 'benchmark/ips'
22

3-
SLUG = 'root_url'
3+
SLUG = "some_kind_of_root_url"
44

5-
def slow
5+
def slower
66
SLUG =~ /_(path|url)$/
77
end
88

9+
def slow
10+
SLUG.match?(/_(path|url)$/)
11+
end
12+
913
def fast
1014
SLUG.end_with?('_path', '_url')
1115
end
1216

1317
Benchmark.ips do |x|
14-
x.report('String#=~') { slow }
18+
x.report('String#=~') { slower }
19+
x.report('String#match?') { slow } if RUBY_VERSION >= "2.4.0".freeze
1520
x.report('String#end_with?') { fast }
1621
x.compare!
1722
end
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
require 'benchmark/ips'
22

3-
SLUG = 'test_reverse_merge.rb'
3+
SLUG = 'test_some_kind_of_long_file_name.rb'
44

5-
def slow
5+
def slower
66
SLUG =~ /^test_/
77
end
88

9+
def slow
10+
SLUG.match?(/^test_/)
11+
end
12+
913
def fast
1014
SLUG.start_with?('test_')
1115
end
1216

1317
Benchmark.ips do |x|
14-
x.report('String#=~') { slow }
18+
x.report('String#=~') { slower }
19+
x.report('String#match?') { slow } if RUBY_VERSION >= "2.4.0".freeze
1520
x.report('String#start_with?') { fast }
1621
x.compare!
1722
end

0 commit comments

Comments
 (0)