File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -764,6 +764,25 @@ String#gsub!'string': 481183.5 i/s - 2.52x slower
764764String#gsub!/regexp/: 342003.8 i/s - 3.55x slower
765765```
766766
767+ ##### ` String#sub ` vs ` String#chomp ` [ code] ( code/string/sub-vs-chomp.rb )
768+
769+ Note that this can only be used for removing characters from the end of a string.
770+
771+ ```
772+ $ ruby -v code/string/sub-vs-chomp.rb
773+ ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]
774+ Calculating -------------------------------------
775+ String#sub/regexp/ 38.068k i/100ms
776+ String#chomp'string' 78.284k i/100ms
777+ -------------------------------------------------
778+ String#sub/regexp/ 560.625k (±17.1%) i/s - 2.703M
779+ String#chomp'string' 2.704M (±18.6%) i/s - 12.839M
780+
781+ Comparison:
782+ String#chomp'string': 2703881.6 i/s
783+ String#sub/regexp/: 560625.4 i/s - 4.82x slower
784+ ```
785+
767786##### ` attr_accessor ` vs ` getter and setter ` [ code] ( code/general/attr-accessor-vs-getter-and-setter.rb )
768787
769788> https://www.omniref.com/ruby/2.2.0/files/method.h?#annotation=4081781&line=47
Original file line number Diff line number Diff line change 1+ require 'benchmark/ips'
2+
3+ SLUG = 'YourSubclassType'
4+
5+ def slow
6+ SLUG . sub ( /Type$/ , '' )
7+ end
8+
9+ def fast
10+ SLUG . chomp ( 'Type' )
11+ end
12+
13+ Benchmark . ips do |x |
14+ x . report ( 'String#sub/regexp/' ) { slow }
15+ x . report ( "String#chomp'string'" ) { fast }
16+ x . compare!
17+ end
You can’t perform that action at this time.
0 commit comments