Created
February 24, 2010 18:27
-
-
Save kelan/313693 to your computer and use it in GitHub Desktop.
Test ruby hash merging speed (#merge vs #merge!). Linked from: http://stackoverflow.com/questions/1972284/is-there-an-idiomatic-way-to-specify-default-values-for-optional-parameters-in-r#comment2298222_1972296
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# To test is using `defaults.merge(options)` or `defaults.merge!(options)` | |
# is faster, I ran the following (manually replacing 'merge' with 'merge!' | |
# as appropriate): | |
10000000.times do | |
defaults = {:a => 1, :b => 2, :c => 3, :d => 4} | |
options = {:a => "another value", :d => true} | |
options = defaults.merge!(options) | |
end | |
puts "done" | |
# And the run-times (in seconds, as reported by TextMate's | |
# script runner) were: | |
# | |
# merge: 56.69, 57.15, 57.26 avg: 57.03 seconds | |
# merge!: 36.04, 36.18, 36.04 avg: 36.09 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment