in reply to comparing arrays
update#!/usr/bin/perl -w use strict; use Data::Compare; use Benchmark; sub aeq { # Zaxo my ($first, $second, @comp) = @_; return 0 if @{$first} != @{$second}; @comp = grep {$first->[$_] ne $second->[$_]} 0..$#$first; return not @comp; } sub compare_arrays { # from FAQ my ($first, $second) = @_; return 0 unless @$first == @$second; for (my $i = 0; $i < @$first; $i++) { return 0 if $first->[$i] ne $second->[$i]; } return 1; } my @first = (qw(a b c d e f), (1..200)) ; my @second= (qw(b a c d e f), (1..200)) ; timethese (20_000, { 'grep' => sub {my $diff = aeq(\@first,\@second)}, 'faq' => sub {my $diff = compare_arrays(\@first,\@second)}, 'DComp' =>sub {my $diff = Compare(\@first,\@second)} }); __END__ Benchmark: timing 20000 iterations of DComp, grep, naif... DComp: 2 wallclock secs ( 1.42 usr + 0.00 sys = 1.42 CPU) grep: 13 wallclock secs (13.13 usr + 0.00 sys = 13.13 CPU) faq: 1 wallclock secs ( 0.55 usr + 0.00 sys = 0.55 CPU)
my @first = ((1..200), qw(a b c d e f)) ; my @second= ((1..200), qw(b a c d e f)) ; Benchmark: timing 2000 iterations of DComp, faq, grep... DComp: 12 wallclock secs (11.52 usr + 0.00 sys = 11.52 CPU) faq: 2 wallclock secs ( 1.48 usr + 0.00 sys = 1.48 CPU) grep: 1 wallclock secs ( 1.35 usr + 0.00 sys = 1.35 CPU)
_ _ _ _ (_|| | |(_|>< _|
|
---|
Replies are listed 'Best First'. | |
---|---|
(MeowChow) Re2: (Efficiently) comparing arrays
by MeowChow (Vicar) on Apr 04, 2002 at 10:15 UTC | |
Re: Re: (Efficiently) comparing arrays
by cyberconte (Scribe) on Apr 04, 2002 at 10:03 UTC | |
by MeowChow (Vicar) on Apr 04, 2002 at 10:19 UTC | |
by demerphq (Chancellor) on Apr 04, 2002 at 14:09 UTC |