Created
January 11, 2019 02:24
-
-
Save naveed-ahmad/8f0b926ffccf5fbd206a1cc58ce9743e to your computer and use it in GitHub Desktop.
Revisions
-
naveed-ahmad created this gist
Jan 11, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,194 @@ require 'benchmark/ips' def find_one_using_group_by_select(array) array.group_by{ |e| e }.detect { |k, v| k if v.size > 1 }&.first end def find_one_using_chunk_select(array) array.sort.chunk{ |e| e }.detect { |e, chunk| chunk.size > 1 }&.first end def find_one_using_count_select(array) array.detect{ |e| array.count(e) > 1 } end def find_one_using_hash_map(array) map = {} dup = nil array.each do |v| map[v] = (map[v] || 0 ) + 1 if map[v] > 1 dup = v break end end return dup end def find_all_using_group_by_select(array) array.group_by{ |e| e }.select { |k, v| k if v.size > 1 }.map &:first end def find_all_using_chunk_select(array) array.sort.chunk{ |e| e }.select { |e, chunk| chunk.size > 1 }.map &:first end def find_all_using_count_select(array) array.select{ |e| array.count(e) > 1 }.uniq end def find_all_using_hash_map(array) map = {} dup = [] array.each do |v| map[v] = (map[v] || 0 ) + 1 if map[v] == 2 dup << v end end return dup end num_array = Array.new(1000) { rand 1000 } char_array = Array.new(20) { (65 + rand(26)).chr } word_array = Array.new(1000) { Array.new(5) { (65 + rand(26)).chr }.join } Benchmark.ips do |x| puts "Find one uniq element from: Number array" x.report('Using group_by.select') do find_one_using_group_by_select(num_array) end x.report('Using sort.chunk.select') do find_one_using_chunk_select(num_array) end x.report('Using count select') do find_one_using_count_select(num_array) end x.report('Using hash map') do find_one_using_hash_map(num_array) end x.compare! end Benchmark.ips do |x| puts "Find one uniq element from: Character array" x.report('Using group_by.select') do find_one_using_group_by_select(char_array) end x.report('Using sort.chunk.select') do find_one_using_chunk_select(char_array) end x.report('Using count select') do find_one_using_count_select(char_array) end x.report('Using hash map') do find_one_using_hash_map(char_array) end x.compare! end Benchmark.ips do |x| puts "Find one uniq element from: Word array" x.report('Using group_by.select') do find_one_using_group_by_select(word_array) end x.report('Using sort.chunk.select') do find_one_using_chunk_select(word_array) end x.report('Using count select') do find_one_using_count_select(word_array) end x.report('Using hash map') do find_one_using_hash_map(word_array) end x.compare! end Benchmark.ips do |x| puts "Find all uniq elements from: Number array" x.report('Using group_by.select') do find_all_using_group_by_select(num_array) end x.report('Using sort.chunk.select') do find_all_using_chunk_select(num_array) end x.report('Using count select') do find_all_using_count_select(num_array) end x.report('Using hash map') do find_all_using_hash_map(num_array) end x.compare! end Benchmark.ips do |x| puts "Find all uniq elements from: Character array" x.report('Using group_by.select') do find_all_using_group_by_select(char_array) end x.report('Using sort.chunk.select') do find_all_using_chunk_select(char_array) end x.report('Using count select') do find_all_using_count_select(char_array) end x.report('Using hash map') do find_all_using_hash_map(char_array) end x.compare! end Benchmark.ips do |x| puts "Find all uniq elements from: Word array" x.report('Using group_by.select') do find_all_using_group_by_select(word_array) end x.report('Using sort.chunk.select') do find_all_using_chunk_select(word_array) end x.report('Using count select') do find_all_using_count_select(word_array) end x.report('Using hash map') do find_all_using_hash_map(word_array) end x.compare! end