Skip to content

Instantly share code, notes, and snippets.

@murajun1978
Created August 27, 2015 13:07
Show Gist options
  • Save murajun1978/06e069eee6061e25e0c2 to your computer and use it in GitHub Desktop.
Save murajun1978/06e069eee6061e25e0c2 to your computer and use it in GitHub Desktop.
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