Created
August 27, 2015 13:07
-
-
Save murajun1978/06e069eee6061e25e0c2 to your computer and use it in GitHub Desktop.
This file contains 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
require 'benchmark/ips' | |
hash_array = (1..10000).map{|n| {name: "foo#{n}"}} | |
Benchmark.ips do |x| | |
x.time = 100 | |
x.report("reverse") do | |
hash_array.sort_by!{|v| v[:name]} | |
hash_array.reverse! | |
end | |
x.report("minus") do | |
hash_array.sort! do |v1, v2| | |
n = v1[:name] <=> v2[:name] | |
n * -1 | |
end | |
end | |
x.compare! | |
end | |
# 実行結果 | |
Calculating ------------------------------------- | |
reverse 35.000 i/100ms | |
minus 43.000 i/100ms | |
------------------------------------------------- | |
reverse 401.677 (± 7.7%) i/s - 2.030k | |
minus 449.311 (±10.2%) i/s - 2.236k | |
Comparison: | |
minus: 449.3 i/s | |
reverse: 401.7 i/s - 1.12x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment