File tree Expand file tree Collapse file tree 2 files changed +4
-26
lines changed Expand file tree Collapse file tree 2 files changed +4
-26
lines changed Original file line number Diff line number Diff line change @@ -143,18 +143,13 @@ ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides-
143
143
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20]
144
144
145
145
Calculating -------------------------------------
146
- using a Hash 8.641M (± 1.9%) i/s - 43.818M in 5.073094s
147
- using a case statement
148
- 8.314M (± 3.1%) i/s - 41.587M in 5.007488s
149
146
using an if statement
150
- 8.295M (± 3.3 %) i/s - 41.524M in 5.012041s
151
- String#constantize 2.365M (± 3.7 %) i/s - 11.884M in 5.032603s
147
+ 8.124M (± 1.8 %) i/s - 41.357M in 5.092437s
148
+ String#constantize 2.462M (± 2.4 %) i/s - 12.315M in 5.004089s
152
149
153
150
Comparison:
154
- using a Hash: 8640697.2 i/s
155
- using a case statement: 8313545.9 i/s - same-ish: difference falls within error
156
- using an if statement: 8295324.8 i/s - same-ish: difference falls within error
157
- String#constantize: 2365366.0 i/s - 3.65x (± 0.00) slower
151
+ using an if statement: 8123851.3 i/s
152
+ String#constantize: 2462371.2 i/s - 3.30x (± 0.00) slower
158
153
```
159
154
160
155
##### ` raise ` vs ` E2MM#Raise ` for raising (and defining) exeptions [ code] ( code/general/raise-vs-e2mmap.rb )
Original file line number Diff line number Diff line change 3
3
4
4
class Foo ; end
5
5
6
- def fast3 ( s , h )
7
- klass = h [ s ]
8
- nil
9
- end
10
-
11
- def fast2 ( s )
12
- klass = case s
13
- when "Foo"
14
- Foo
15
- end
16
- nil
17
- end
18
-
19
6
def fast ( s )
20
7
klass = Foo if s == "Foo"
21
8
nil
@@ -27,10 +14,6 @@ def slow(s)
27
14
end
28
15
29
16
Benchmark . ips do |x |
30
- h = { "Foo" => Foo }
31
-
32
- x . report ( "using a Hash" ) { fast3 ( "Foo" , h ) }
33
- x . report ( "using a case statement" ) { fast2 ( "Foo" ) }
34
17
x . report ( "using an if statement" ) { fast ( "Foo" ) }
35
18
x . report ( "String#constantize" ) { slow ( "Foo" ) }
36
19
x . compare!
You can’t perform that action at this time.
0 commit comments