File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -197,7 +197,7 @@ Comparison:
197197 For loop: 198517.3 i/s - 1.05x slower
198198```
199199
200- ##### ` Enumerable#each_with_index ` vs ` while ` loop [ code] ( code/array /each_with_index-vs-while-loop.rb )
200+ ##### ` Enumerable#each_with_index ` vs ` while ` loop [ code] ( code/enumerable /each_with_index-vs-while-loop.rb )
201201
202202> [ rails/rails #12065 ] ( https://github.com/rails/rails/pull/12065 )
203203
@@ -547,8 +547,24 @@ Comparison:
547547 String#gsub: 516604.2 i/s - 3.60x slower
548548```
549549
550+ ##### ` attr_accessor ` vs ` getter and setter ` [ code] ( code/general/attr-accessor-vs-getter-and-setter.rb )
550551
552+ > https://www.omniref.com/ruby/2.2.0/files/method.h?#annotation=4081781&line=47
551553
554+ ```
555+ $ ruby -v code/general/attr-accessor-vs-getter-and-setter.rb
556+ ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
557+ Calculating -------------------------------------
558+ getter_and_setter 61.240k i/100ms
559+ attr_accessor 66.535k i/100ms
560+ -------------------------------------------------
561+ getter_and_setter 1.660M (± 9.7%) i/s - 8.267M
562+ attr_accessor 1.865M (± 9.2%) i/s - 9.248M
563+
564+ Comparison:
565+ attr_accessor: 1865408.4 i/s
566+ getter_and_setter: 1660021.9 i/s - 1.12x slower
567+ ```
552568
553569## Submit New Entry
554570
Original file line number Diff line number Diff line change 1+ require 'benchmark/ips'
2+
3+ class User
4+ attr_accessor :first_name
5+
6+ def last_name
7+ @last_name
8+ end
9+
10+ def last_name = ( value )
11+ @last_name = value
12+ end
13+
14+ end
15+
16+ def slow
17+ user = User . new
18+ user . last_name = 'John'
19+ user . last_name
20+ end
21+
22+ def fast
23+ user = User . new
24+ user . first_name = 'John'
25+ user . first_name
26+ end
27+
28+ Benchmark . ips do |x |
29+ x . report ( 'getter_and_setter' ) { slow }
30+ x . report ( 'attr_accessor' ) { fast }
31+ x . compare!
32+ end
You can’t perform that action at this time.
0 commit comments