Skip to content

Commit 0780071

Browse files
committed
simplify
1 parent 5f45d63 commit 0780071

File tree

2 files changed

+4
-26
lines changed

2 files changed

+4
-26
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,13 @@ ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides-
143143
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20]
144144
145145
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
149146
using an if statement
150-
8.295M3.3%) i/s - 41.524M in 5.012041s
151-
String#constantize 2.365M3.7%) i/s - 11.884M in 5.032603s
147+
8.124M1.8%) i/s - 41.357M in 5.092437s
148+
String#constantize 2.462M2.4%) i/s - 12.315M in 5.004089s
152149
153150
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
158153
```
159154

160155
##### `raise` vs `E2MM#Raise` for raising (and defining) exeptions [code](code/general/raise-vs-e2mmap.rb)

code/general/constantize-vs-comparison.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33

44
class 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-
196
def fast(s)
207
klass = Foo if s == "Foo"
218
nil
@@ -27,10 +14,6 @@ def slow(s)
2714
end
2815

2916
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") }
3417
x.report("using an if statement") { fast("Foo") }
3518
x.report("String#constantize") { slow("Foo") }
3619
x.compare!

0 commit comments

Comments
 (0)