Skip to content

Instantly share code, notes, and snippets.

@equivalent
Last active June 21, 2021 10:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equivalent/3c9a4c9d07fff79062a3 to your computer and use it in GitHub Desktop.
Save equivalent/3c9a4c9d07fff79062a3 to your computer and use it in GitHub Desktop.
fetch elements that occurred multiple times in Ruby Array
require 'benchmark'
require 'faker'
ary = []
100.times do
ary << Faker::Name.name
end
n = 100_000
Benchmark.bmbm(20) do |r|
r.report('Using group_by.select') do
n.times do
ary
.group_by{ |e| e }
.select { |k, v| v.size > 1 }
.map(&:first)
end
end
r.report('Using sort.chunk.select') do
n.times do
ary
.sort
.chunk{ |e| e }
.select { |e, chunk| chunk.size > 1 }
.map(&:first)
end
end
r.report('Using count select') do
n.times do
ary.select{ |e| ary.count(e) > 1 }.uniq
end
end
end
# running or ruby 2.2.2
#
# Rehearsal -----------------------------------------------------------
# Using group_by.select 5.350000 0.000000 5.350000 ( 5.356332)
# Using sort.chunk.select 7.640000 0.000000 7.640000 ( 7.641986)
# Using count select 45.320000 0.030000 45.350000 ( 45.320775)
# ------------------------------------------------- total: 58.340000sec
#
# user system total real
# Using group_by.select 5.460000 0.000000 5.460000 ( 5.457235)
# Using sort.chunk.select 7.670000 0.000000 7.670000 ( 7.664146)
# Using count select 45.490000 0.030000 45.520000 ( 45.488224)
require 'benchmark'
ary = ["A", "B", "C", "B", "A", "D", "E", "F", "G", "H"]
n = 100_000
Benchmark.bmbm(20) do |r|
r.report('Using group_by.select') do
n.times do
ary
.group_by{ |e| e }
.select { |k, v| v.size > 1 }
.map(&:first)
end
end
r.report('Using sort.chunk.select') do
n.times do
ary
.sort
.chunk{ |e| e }
.select { |e, chunk| chunk.size > 1 }
.map(&:first)
end
end
r.report('Using count select') do
n.times do
ary.select{ |e| ary.count(e) > 1 }.uniq
end
end
end
# on Ruby 2.1.2
#
# Rehearsal -----------------------------------------------------------
# Using group_by.select 0.720000 0.000000 0.720000 ( 0.717274)
# Using sort.chunk.select 1.510000 0.000000 1.510000 ( 1.506170)
# Using count select 0.730000 0.000000 0.730000 ( 0.737882)
# -------------------------------------------------- total: 2.960000sec
#
# user system total real
# Using group_by.select 0.790000 0.000000 0.790000 ( 0.790971)
# Using sort.chunk.select 1.500000 0.000000 1.500000 ( 1.498845)
# Using count select 0.720000 0.000000 0.720000 ( 0.725994)
# on Ruby 2.2.2
#
# Using group_by.select 0.640000 0.010000 0.650000 ( 0.645127)
# Using sort.chunk.select 1.370000 0.000000 1.370000 ( 1.365221)
# Using count select 0.730000 0.000000 0.730000 ( 0.735518)
#-------------------------------------------------- total: 2.750000sec
#
# user system total real
# Using group_by.select 0.710000 0.000000 0.710000 ( 0.710802)
# Using sort.chunk.select 1.380000 0.000000 1.380000 ( 1.385528)
# Using count select 0.720000 0.000000 0.720000 ( 0.710731)
@elissonmichael
Copy link

Thank you for sharing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment