Created
October 10, 2017 09:18
-
-
Save holysugar/3244d84ec4766fc664c06cfab8c9941d 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 = 20000 | |
m = 100 | |
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 }} | |
end | |
# user system total real | |
# 1: 0.380000 0.000000 0.380000 ( 0.390095) | |
# 2: 0.310000 0.000000 0.310000 ( 0.323532) | |
# 3: 0.150000 0.000000 0.150000 ( 0.152381) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment