@@ -1182,23 +1182,42 @@ String#gsub!'string': 481183.5 i/s - 2.52x slower
1182
1182
String#gsub!/regexp/: 342003.8 i/s - 3.55x slower
1183
1183
```
1184
1184
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 )
1186
1186
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.
1187
1207
Note that this can only be used for removing characters from the end of a string.
1188
1208
1189
1209
```
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 ]
1192
1212
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
1198
1216
1199
1217
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
1202
1221
```
1203
1222
1204
1223
##### ` String#unpack1 ` vs ` String#unpack[0] ` [ code] ( code/string/unpack1-vs-unpack[0].rb )
0 commit comments