develooper Front page | perl.beginners | Postings from February 2009

How to speed up two arrays compare.

Thread Next
From:
kevin liu
Date:
February 11, 2009 00:05
Subject:
How to speed up two arrays compare.
Message ID:
9618a85a0902110004t14a0553cg7ff6d16e0af1c34c@mail.gmail.com
Hi everybody:

        I have two arrays(@nwarray0 and @nwarray1) in my program and i want
to make sure that
all the elements in @nwarray0 could be found in @nwarray1.
        Here is my implementation:
        -------------------------------------------------------
        foreach my $srctemp ( @nwarray0 ) {

            foreach my $tgttemp ( @nwarray1 ) {
                if ( $tgttemp eq $srctemp ) {
                    $found = 1;
                    last;
                }
            }
            if ( $found == 1 ) {
                $found = 0;
                next;
            }
            else {
                return 1;
            }
        }
        --------------------------------------------------------
        But this algorithm takes a long time to compare, could you please
help to improve this piece of
code to less the time needed?

        Thanks in advance.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About