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-
143143ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20]
144144
145145Calculating -------------------------------------
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
149146using 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
152149
153150Comparison:
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
158153```
159154
160155##### ` 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 33
44class Foo ; end
55
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-
196def fast ( s )
207 klass = Foo if s == "Foo"
218 nil
@@ -27,10 +14,6 @@ def slow(s)
2714end
2815
2916Benchmark . 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" ) }
3417 x . report ( "using an if statement" ) { fast ( "Foo" ) }
3518 x . report ( "String#constantize" ) { slow ( "Foo" ) }
3619 x . compare!
You can’t perform that action at this time.
0 commit comments