Last active
May 14, 2025 03:21
-
-
Save kejadlen/03ce4379a40a938d3c95b7bf4cbbdaa9 to your computer and use it in GitHub Desktop.
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 characters
require "benchmark" | |
def func_a(*arrays) | |
enumerators = arrays.map {|a| (a.each + [Float::INFINITY]).each } | |
output = Set.new | |
until enumerators.all? { _1.peek.infinite? } | |
output << enumerators.min {|a,b| a.peek <=> b.peek }.next | |
end | |
output | |
end | |
def func_b(*arrays) | |
arrays.reduce(:+).sort.uniq | |
end | |
def func_bb(*arrays) | |
arrays.reduce(:+).uniq | |
end | |
def func_c(*arrays) | |
Set.new.merge(*arrays) | |
end | |
arrays = 3.times.map { Array.new(10_000) { rand(1_000) }} | |
Benchmark.bmbm do |x| | |
x.report("func_a") { func_a(*arrays) } | |
x.report("func_b") { func_b(*arrays) } | |
x.report("func_bb") { func_bb(*arrays) } | |
x.report("func_c") { func_c(*arrays) } | |
end | |
__END__ | |
Rehearsal ------------------------------------------- | |
func_a 0.014743 0.000486 0.015229 ( 0.015439) | |
func_b 0.001939 0.000121 0.002060 ( 0.002069) | |
func_bb 0.000421 0.000046 0.000467 ( 0.000479) | |
func_c 0.001669 0.000023 0.001692 ( 0.001699) | |
---------------------------------- total: 0.019448sec | |
user system total real | |
func_a 0.012642 0.000113 0.012755 ( 0.012797) | |
func_b 0.001905 0.000114 0.002019 ( 0.002026) | |
func_bb 0.000311 0.000062 0.000373 ( 0.000372) | |
func_c 0.001587 0.000021 0.001608 ( 0.001659) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment