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

Adds `Enumerable#each_with_object` vs `Enumerable#inject` comparison #210

Closed mateusdeap closed 1 year ago

mateusdeap commented 2 years ago

Solves #6

KostiantynPopovych commented 2 years ago

Hey @rishijain, can you review this PR, please?

mateusdeap commented 1 year ago

Yup, definitely, rerunning the example, the performance is just about the same. Will close this PR and corresponding issue. For future reference, here are the updated results:

ruby -v code/enumerable/each-with-object-vs-inject.rb
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
Warming up --------------------------------------
Enumerable#each_with_object
                         1.044k i/100ms
   Enumerable#inject     1.033k i/100ms
Calculating -------------------------------------
Enumerable#each_with_object
                         10.554k (± 1.2%) i/s -     53.244k in   5.045839s
   Enumerable#inject     10.479k (± 0.7%) i/s -     52.683k in   5.027723s

Comparison:
Enumerable#each_with_object:    10553.7 i/s
   Enumerable#inject:    10479.0 i/s - same-ish: difference falls within error