fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

assignment for array elements #33

Closed u2 closed 8 years ago

u2 commented 9 years ago

Inspired by avoid run time allocations

ruby -v code/general/assignment_array_elements.rb
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
Calculating -------------------------------------
element objects Assignment
                        83.252k i/100ms
    array Assignment    81.089k i/100ms
-------------------------------------------------
element objects Assignment
                          2.372M (±10.8%) i/s -     11.739M
    array Assignment      2.875M (± 8.2%) i/s -     14.272M

Comparison:
    array Assignment:  2875123.2 i/s
element objects Assignment:  2371948.5 i/s - 1.21x slower

Calculating -------------------------------------
element objects Assignment
                        81.927k i/100ms
    array Assignment    91.828k i/100ms
-------------------------------------------------
element objects Assignment
                          2.316M (± 8.8%) i/s -     11.552M
    array Assignment      3.801M (±11.8%) i/s -     18.641M

Comparison:
    array Assignment:  3800505.2 i/s
element objects Assignment:  2316031.8 i/s - 1.64x slower
winston commented 8 years ago

Hi! Pardon me, as I am helping @JuanitoFatas to clear the PRs.

I think we would prefer to merge comparison between atomic and generic actions. The actions performed in this PR include access and delete, and seems to be more logic-dependent than generic in nature. So I would lean towards closing this PR. :kissing_heart:

ixti commented 8 years ago

I would also like to point out that comparison is incorrect. In slow version you are creating a new (!) array at the end instead of returning original env. With this patch:

--- assignment_array_elements.rb    2016-02-10 21:21:27.403563391 +0100
+++ assignment_array_elements_fixed.rb  2016-02-10 21:22:02.691562471 +0100
@@ -3,7 +3,7 @@
 def slow(env)
   status, headers, body = env
   headers.delete('Http-Access-Token')
-  [status, headers, body]
+  env
 end

 def fast(env)
@@ -22,7 +22,7 @@
 def slow_ext(env)
   status, headers, body = env
   headers['X-Clacks-Overhead'] = 'GNU Terry Pratchett'
-  [status, headers, body]
+  env
 end

 def fast_ext(env)

Results will become:

Calculating -------------------------------------
element objects Assignment
                       115.648k i/100ms
    array Assignment   126.859k i/100ms
-------------------------------------------------
element objects Assignment
                          4.492M (± 6.2%) i/s -     22.320M
    array Assignment      4.840M (± 0.7%) i/s -     24.230M

Comparison:
    array Assignment:  4840436.0 i/s
element objects Assignment:  4491909.9 i/s - 1.08x slower

Calculating -------------------------------------
element objects Assignment
                       127.003k i/100ms
    array Assignment   131.268k i/100ms
-------------------------------------------------
element objects Assignment
                          4.529M (± 3.4%) i/s -     22.607M
    array Assignment      5.262M (± 2.7%) i/s -     26.385M

Comparison:
    array Assignment:  5262202.4 i/s
element objects Assignment:  4529275.0 i/s - 1.16x slower