Skip to content

Commit c2b3ce2

Browse files
authored
Merge pull request #152 from bdewater/string-delete-prefix
Add `String#delete_prefix` and `delete_suffix` benchmarks
2 parents fb7e06e + efca7f6 commit c2b3ce2

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ sudo: false
22
cache: bundler
33
bundler_args: --retry=3 --jobs=3
44
language: ruby
5+
before_install:
6+
# https://github.com/travis-ci/travis-ci/issues/8978#issuecomment-354036443
7+
- gem update --system
8+
- gem install bundler
59
rvm:
10+
- 2.5.1
611
- 2.4.3
712
- 2.3.0
813
- 2.1.8

README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,23 +1182,42 @@ String#gsub!'string': 481183.5 i/s - 2.52x slower
11821182
String#gsub!/regexp/: 342003.8 i/s - 3.55x slower
11831183
```
11841184

1185-
##### `String#sub` vs `String#chomp` [code](code/string/sub-vs-chomp.rb)
1185+
##### `String#sub` vs `String#delete_prefix` [code](code/string/sub-vs-delete_prefix.rb)
11861186

1187+
[Ruby 2.5 introduced](https://bugs.ruby-lang.org/issues/12694) `String#delete_prefix`.
1188+
Note that this can only be used for removing characters from the start of a string.
1189+
1190+
```
1191+
$ ruby -v code/string/sub-vs-delete_prefix.rb
1192+
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
1193+
Calculating -------------------------------------
1194+
String#delete_prefix 4.112M (± 1.8%) i/s - 20.707M in 5.037928s
1195+
String#sub 814.725k (± 1.4%) i/s - 4.088M in 5.018962s
1196+
1197+
Comparison:
1198+
String#delete_prefix: 4111531.1 i/s
1199+
String#sub: 814725.3 i/s - 5.05x slower
1200+
```
1201+
1202+
##### `String#sub` vs `String#chomp` vs `String#delete_suffix` [code](code/string/sub-vs-chomp-vs-delete_suffix.rb)
1203+
1204+
[Ruby 2.5 introduced](https://bugs.ruby-lang.org/issues/13665) `String#delete_suffix`
1205+
as a counterpart to `delete_prefix`. The performance gain over `chomp` is
1206+
small and during some runs the difference falls within the error margin.
11871207
Note that this can only be used for removing characters from the end of a string.
11881208

11891209
```
1190-
$ ruby -v code/string/sub-vs-chomp.rb
1191-
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin13]
1210+
$ ruby -v code/string/sub-vs-chomp-vs-delete_suffix.rb
1211+
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
11921212
Calculating -------------------------------------
1193-
String#sub/regexp/ 42.816k i/100ms
1194-
String#chomp'string' 94.851k i/100ms
1195-
-------------------------------------------------
1196-
String#sub/regexp/ 660.509k (± 8.0%) i/s - 3.297M
1197-
String#chomp'string' 2.803M (± 8.0%) i/s - 13.943M
1213+
String#sub 838.415k (± 1.7%) i/s - 4.214M in 5.027412s
1214+
String#chomp 3.951M (± 2.1%) i/s - 19.813M in 5.017089s
1215+
String#delete_suffix 4.202M (± 2.1%) i/s - 21.075M in 5.017429s
11981216
11991217
Comparison:
1200-
String#chomp'string': 2803443.5 i/s
1201-
String#sub/regexp/: 660508.7 i/s - 4.24x slower
1218+
String#delete_suffix: 4202201.7 i/s
1219+
String#chomp: 3950921.9 i/s - 1.06x slower
1220+
String#sub: 838415.3 i/s - 5.01x slower
12021221
```
12031222

12041223
##### `String#unpack1` vs `String#unpack[0]` [code](code/string/unpack1-vs-unpack[0].rb)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'benchmark/ips'
2+
3+
SLUG = 'YourSubclassType'
4+
5+
def slow
6+
SLUG.sub(/Type\z/, '')
7+
end
8+
9+
def fast
10+
SLUG.chomp('Type')
11+
end
12+
13+
def faster
14+
SLUG.delete_suffix('Type')
15+
end
16+
17+
Benchmark.ips do |x|
18+
x.report('String#sub') { slow }
19+
x.report("String#chomp") { fast }
20+
x.report("String#delete_suffix") { faster } if RUBY_VERSION >= '2.5.0'
21+
x.compare!
22+
end

code/string/sub-vs-chomp.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

code/string/sub-vs-delete_prefix.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "benchmark/ips"
2+
3+
if RUBY_VERSION >= '2.5.0'
4+
STRING = 'Foo::Foo::Bar'.freeze
5+
6+
def fast
7+
STRING.delete_prefix('Foo::')
8+
end
9+
10+
def slow
11+
STRING.sub(/\AFoo::/, '')
12+
end
13+
14+
Benchmark.ips do |x|
15+
x.report("String#delete_prefix") { fast }
16+
x.report('String#sub') { slow }
17+
x.compare!
18+
end
19+
end

0 commit comments

Comments
 (0)