in reply to Re^2: better union of sets algorithm?
in thread better union of sets algorithm?

Yes, you are correct. How embaracing. The timing remains about the same though.
Union_1 96235
Union_2 96235
            (warning: too few iterations for a reliable count)
         s/iter   sorted unsorted
sorted     4.39       --     -66%
unsorted   1.48     196%       --
Union_1 96235
Union_2 96235
Union a b c d g e f
Union a b c d e f g
           Rate unsorted   sorted
unsorted  202/s       --     -92%
sorted   2511/s    1144%       --
Union a b c d g e f
Union a b c d e f g
sub union_1 { my $count = shift; $count--; my %saw; grep(0 == $saw{$_}++, @_); } sub union_2 { my $self = [@_]; my ($lowest_arr, $lowest, $next); my @ret = (); my $elements = scalar @_; while (1) { my $count = 0; my $work = 0; my $lowest; foreach my $a (@$self) { next unless (@$a); if (!defined($lowest) or ($a->[0] le $lowest)) { $lowest = $a->[0]; } } foreach my $a (@$self) { next unless (@$a); if ($a->[0] eq $lowest) { shift @$a; $work++; } } return @ret unless $lowest; push(@ret, $lowest); } }
A picture is worth a thousand words, but takes 200K.