Last active
December 27, 2015 12:19
-
-
Save igorkasyanchuk/7324745 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' | |
F = Struct.new(:name, :size) do | |
def grouping | |
"#{name}/#{size}" | |
end | |
end | |
f0 = F.new("a.txt", 110) | |
f1 = F.new("a.txt", 10) | |
f2 = F.new("b.txt", 10) | |
f3 = F.new("a.txt", 110) | |
f4 = F.new("d.txt", 11) | |
FILES = [f0, f1, f2, f3, f4] | |
N = 100_000 | |
Benchmark.bm do |x| | |
x.report("block") { N.times { Hash[FILES.group_by{|e|"#{e.name}/#{e.size}"}.select{|k,v| v.size > 1}.collect{|k,v| [k.split('/')[0],v]}] } } | |
x.report("method") { N.times { Hash[FILES.group_by(&:grouping).select{|k,v| v.size > 1}.collect{|k,v| [k.split('/')[0],v]}] } } | |
x.report("block reject") { N.times { Hash[FILES.group_by{|e|"#{e.name}/#{e.size}"}.reject{|k,v| v.size <= 1}.collect{|k,v| [k.split('/')[0],v]}] } } | |
x.report("inject") { N.times { FILES.group_by{|e|"#{e.name}/#{e.size}"}.inject({}){|res, (k, v)| res[k.split('/')[0]] = v if v.size > 1; res} } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment