Last active
October 10, 2017 09:33
-
-
Save holysugar/084afbe2aea27ef0510629d047b46262 to your computer and use it in GitHub Desktop.
today's bench
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' | |
require 'active_support/all' | |
n = ARGV[0]&.to_i || 50000 | |
m = ARGV[1]&.to_i || 20 | |
arr = Array.new(m){|i| [i, [:a, :b, :c, :d].sample] }.shuffle.to_h | |
Benchmark.bmbm do |b| | |
b.report("1:") { n.times{ arr.select{|_,v| v == :a }.max&.first }} | |
b.report("2:") { n.times{ arr.each_with_object([]){|(k,v),o| o << k if v == :a }.max }} | |
b.report("3:") { n.times{ a = 0; arr.each{|k,v| a = k if v == :a && a < k }; a == 0 ? nil : a }} | |
b.report("4:") { n.times{ arr.sort.reverse_each.find{|k,v| v == :a }&.first }} | |
b.report("5:") { n.times{ arr.sort_by{|k,v| -k }.find{|k,v| v == :a }&.first }} | |
end | |
__END__ | |
Rehearsal -------------------------------------- | |
1: 0.190000 0.000000 0.190000 ( 0.191267) | |
2: 0.180000 0.010000 0.190000 ( 0.185720) | |
3: 0.080000 0.000000 0.080000 ( 0.085030) | |
4: 0.970000 0.000000 0.970000 ( 0.988984) | |
5: 0.430000 0.010000 0.440000 ( 0.439052) | |
----------------------------- total: 1.870000sec | |
user system total real | |
1: 0.180000 0.000000 0.180000 ( 0.189173) | |
2: 0.180000 0.000000 0.180000 ( 0.185014) | |
3: 0.090000 0.000000 0.090000 ( 0.091285) | |
4: 0.990000 0.010000 1.000000 ( 1.003560) | |
5: 0.420000 0.000000 0.420000 ( 0.426253) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment