From 5f45d63c3222fa9a9cd6544b34d201e989350212 Mon Sep 17 00:00:00 2001 From: Will Saylor Date: Mon, 6 Jun 2022 16:34:00 -0400 Subject: [PATCH 1/4] constantize vs comparison --- README.md | 24 +++++++++++++++ code/general/constantize-vs-comparison.rb | 37 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 code/general/constantize-vs-comparison.rb diff --git a/README.md b/README.md index b63f1c9..0b3bc79 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,30 @@ Comparison: module_eval with string: 1129.7 i/s - 1.19x slower ``` +##### `String#constantize` vs a comparison for inflection + +ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides-4.2.1-en/active_support_core_extensions.html#constantize) "resolves the constant reference expression in its receiver". + +[Read the rationale here](asdf) + +``` +ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20] + +Calculating ------------------------------------- + using a Hash 8.641M (± 1.9%) i/s - 43.818M in 5.073094s +using a case statement + 8.314M (± 3.1%) i/s - 41.587M in 5.007488s +using an if statement + 8.295M (± 3.3%) i/s - 41.524M in 5.012041s + String#constantize 2.365M (± 3.7%) i/s - 11.884M in 5.032603s + +Comparison: + using a Hash: 8640697.2 i/s +using a case statement: 8313545.9 i/s - same-ish: difference falls within error +using an if statement: 8295324.8 i/s - same-ish: difference falls within error + String#constantize: 2365366.0 i/s - 3.65x (± 0.00) slower +``` + ##### `raise` vs `E2MM#Raise` for raising (and defining) exeptions [code](code/general/raise-vs-e2mmap.rb) Ruby's [Exception2MessageMapper module](http://ruby-doc.org/stdlib-2.2.0/libdoc/e2mmap/rdoc/index.html) allows one to define and raise exceptions with predefined messages. diff --git a/code/general/constantize-vs-comparison.rb b/code/general/constantize-vs-comparison.rb new file mode 100644 index 0000000..82edbe9 --- /dev/null +++ b/code/general/constantize-vs-comparison.rb @@ -0,0 +1,37 @@ +require "active_support/core_ext/string/inflections.rb" +require "benchmark/ips" + +class Foo; end + +def fast3(s, h) + klass = h[s] + nil +end + +def fast2(s) + klass = case s + when "Foo" + Foo + end + nil +end + +def fast(s) + klass = Foo if s == "Foo" + nil +end + +def slow(s) + klass = s.constantize + nil +end + +Benchmark.ips do |x| + h = { "Foo" => Foo } + + x.report("using a Hash") { fast3("Foo", h) } + x.report("using a case statement") { fast2("Foo") } + x.report("using an if statement") { fast("Foo") } + x.report("String#constantize") { slow("Foo") } + x.compare! +end From 0780071072c7dcc8bf2bf996ba65732adcb85a4a Mon Sep 17 00:00:00 2001 From: Will Saylor Date: Mon, 6 Jun 2022 16:42:06 -0400 Subject: [PATCH 2/4] simplify --- README.md | 13 ++++--------- code/general/constantize-vs-comparison.rb | 17 ----------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 0b3bc79..d8e6f30 100644 --- a/README.md +++ b/README.md @@ -143,18 +143,13 @@ ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides- ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20] Calculating ------------------------------------- - using a Hash 8.641M (± 1.9%) i/s - 43.818M in 5.073094s -using a case statement - 8.314M (± 3.1%) i/s - 41.587M in 5.007488s using an if statement - 8.295M (± 3.3%) i/s - 41.524M in 5.012041s - String#constantize 2.365M (± 3.7%) i/s - 11.884M in 5.032603s + 8.124M (± 1.8%) i/s - 41.357M in 5.092437s + String#constantize 2.462M (± 2.4%) i/s - 12.315M in 5.004089s Comparison: - using a Hash: 8640697.2 i/s -using a case statement: 8313545.9 i/s - same-ish: difference falls within error -using an if statement: 8295324.8 i/s - same-ish: difference falls within error - String#constantize: 2365366.0 i/s - 3.65x (± 0.00) slower +using an if statement: 8123851.3 i/s + String#constantize: 2462371.2 i/s - 3.30x (± 0.00) slower ``` ##### `raise` vs `E2MM#Raise` for raising (and defining) exeptions [code](code/general/raise-vs-e2mmap.rb) diff --git a/code/general/constantize-vs-comparison.rb b/code/general/constantize-vs-comparison.rb index 82edbe9..ab797b4 100644 --- a/code/general/constantize-vs-comparison.rb +++ b/code/general/constantize-vs-comparison.rb @@ -3,19 +3,6 @@ class Foo; end -def fast3(s, h) - klass = h[s] - nil -end - -def fast2(s) - klass = case s - when "Foo" - Foo - end - nil -end - def fast(s) klass = Foo if s == "Foo" nil @@ -27,10 +14,6 @@ def slow(s) end Benchmark.ips do |x| - h = { "Foo" => Foo } - - x.report("using a Hash") { fast3("Foo", h) } - x.report("using a case statement") { fast2("Foo") } x.report("using an if statement") { fast("Foo") } x.report("String#constantize") { slow("Foo") } x.compare! From 4cea6d5573d53d1a1d8cd62d25ccc017938b8a81 Mon Sep 17 00:00:00 2001 From: Will Saylor Date: Mon, 6 Jun 2022 16:47:59 -0400 Subject: [PATCH 3/4] Add link to rationale --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8e6f30..f3f2561 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ module_eval with string: 1129.7 i/s - 1.19x slower ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides-4.2.1-en/active_support_core_extensions.html#constantize) "resolves the constant reference expression in its receiver". -[Read the rationale here](asdf) +[Read the rationale here](https://github.com/fastruby/fast-ruby/pull/200) ``` ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20] From c0ac0fedfae5d523487a29d9bc14fb2107f2f5a8 Mon Sep 17 00:00:00 2001 From: Will Saylor Date: Mon, 6 Jun 2022 16:52:36 -0400 Subject: [PATCH 4/4] use guides.rubyonrails.org --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f3f2561..63b02f1 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ module_eval with string: 1129.7 i/s - 1.19x slower ##### `String#constantize` vs a comparison for inflection -ActiveSupport's [String#constantize](https://doc.bccnsoft.com/docs/rails-guides-4.2.1-en/active_support_core_extensions.html#constantize) "resolves the constant reference expression in its receiver". +ActiveSupport's [String#constantize](https://guides.rubyonrails.org/active_support_core_extensions.html#constantize) "resolves the constant reference expression in its receiver". [Read the rationale here](https://github.com/fastruby/fast-ruby/pull/200)