Skip to content

Instantly share code, notes, and snippets.

@jaredbeck
Created October 17, 2021 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredbeck/edc708df10fcc0267db80bf1c31c8298 to your computer and use it in GitHub Desktop.
Save jaredbeck/edc708df10fcc0267db80bf1c31c8298 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'matrix'
v1 = [1, 1, 1, 1]
v2 = [2, 3, 2, 3]
v3 = [1, 1, 1, 1 ]
def sol_1(a,b,c)
sum = 0
zip = a.zip(b, c)
zip.group_by { |e| e}
.select { |_, value| value.size > 1 }
.each_value { |value| sum += (value.size - 1) }
return sum
end
def sol_2(a,b,c)
zip = a.zip(b, c)
hash = Hash.new(0)
zip.each { |e| hash.store(e, hash[e]+1) }
hash.each{|e, _| hash[e] -= 1}
return hash.sum {|e, _| hash[e] }
end
def sol_3(matrix)
Matrix.
columns(matrix).
to_a.
each_with_object({}) { |e, a|
digest = e.hash
a[digest] = a[digest].nil? ? 1 : a[digest] + 1
}.sum { |_, v| v > 1 ? 1 : 0 }
end
n=1_000
Benchmark.bmbm do |x|
x.report("sol_1"){n.times{sol_1(v1, v2, v3)} }
x.report("sol_2"){n.times{sol_2(v1, v2, v3)} }
x.report("sol_3"){n.times{sol_3([v1, v2, v3])} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment