Fast Ruby for hash_update_vs_hash_brackets
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 50/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- performance
Research direction
Use the supplied benchmark-ips example as the starting point and compare Hash#[]= with Hash#update in the existing fast-ruby benchmark collection. Add the corresponding code/hash/update-vs-[]=.rb entry with the shown Ruby benchmark output; done means the comparison is documented alongside merge_bang_vs_[]=.
Written by the indexing model from the issue text.
Description
Hash#update is an alias for Hash#merge!
Currently we have merge_bang_vs_[]= which suggests that Hash#[]= is faster and Hash#merge!() is slower.
But we dont have any suggestions for update_vs_[]= i.e., Hash#update() vs Hash#[]=.
require 'benchmark/ips'
ENUM = (1..100)
def fast
ENUM.each_with_object({}) do |e, h|
h[e] = e
end
end
def slow
ENUM.each_with_object({}) do |e, h|
h.update(e => e)
end
end
Benchmark.ips do |x|
x.report('Hash#[]=') { fast }
x.report('Hash#update') { slow }
x.compare!
end
##### `Hash#update` vs `Hash#[]=` [code](code/hash/update-vs-\[\]=.rb)
$ ruby -v code/hash/update-vs-\[\]=.rb
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin18]
Warming up --------------------------------------
Hash#[]= 7.453k i/100ms
Hash#update 4.311k i/100ms
Calculating -------------------------------------
Hash#[]= 74.764k (± 1.9%) i/s - 380.103k in 5.085962s
Hash#update 43.220k (± 0.8%) i/s - 219.861k in 5.087364s
Comparison:
Hash#[]=: 74764.0 i/s
Hash#update: 43220.1 i/s - 1.73x (± 0.00) slower
Can we extend this to Hash#update()?
- Dominant language
- Ruby
- Stars
- 5.7k
- Forks
- 370
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastruby/fast-ruby
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
All issues in fastruby/fast-ruby
Similar issues
-
SyncEm always forwards a dummy block, so wrapped methods lose their no-block/Enumerator behavior Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
endoflife-date/endoflife.date#11086 ·
-
internal
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Kong/developer.konghq.com#7322 ·
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100