#!/usr/bin/perl use Benchmark; $|=1; @list1 = (); @list2 = (); for $i (1..10000) { push @list1, int rand 100000; push @list2, int rand 100000; } print ".."; @list1 = sort @list1; @list2 = sort @list2; timethis(10, q{%list1 = map {$_, 1} @list1; %list2 = map {$_, 1} @list2; print ".";}); sub set_2_and { my ($words1, $words2, $cmp_fn) = @_; my ($i1, $i2) = (0, 0); my @and = (); while ($i1 < @$words1 and $i2 < @$words2) { my $cmp = $words1->[$i1] <=> $words2->[$i2]; if ($cmp == 0) { push @and, $words1->[$i1]; ++$i1; ++$i2 } elsif ($cmp < 0) { ++$i1 } else { ++$i2 } } return [@and]; }