in reply to improve performance

Whenever you want a fast lookup, try to use a hash. This will only work if your elements exist as single lines instead of actual substring matches. Also note that if $strfile contains regex meta characters (like *, . or +, or []) your code will not work as you might think.

Using a hash would result in:

my %lookup = map { $_ => 1 } @arraytocompare; foreach my $strfile (@tempfiles) { if( $lookup{ $strfile } ) { push (@newarray, $strfile); }; };

But I really, really doubt that searching through 8000 array entries will slow your program down that much. Are you certain that this is where your performance bottleneck is?

Replies are listed 'Best First'.
Re^2: improve performance
by swissknife (Sexton) on Jun 08, 2015 at 10:03 UTC

    Thanks Corion. I added few prints and used executed the script using -d option. which clearly shows that this is where the performance bottelneck is.

    you said if $strfile contains regex meta charachters my code will not will not work as i might have thought... file name has "." before the extension of file. is this statement is valid even if i use hash?

      You may want to take a look at Devel::NYTProf for profiling your code.

Re^2: improve performance
by swissknife (Sexton) on Jun 08, 2015 at 10:41 UTC

    I updated the codes with your suggestion which is really faster but does not give the same result as the grep. i find @newarray empty where as it is not. did you consider the NOT operator ! in my original code?

      No, I did not consider it, but you can consider it in your code.

        event after making the change @newarray is empty.

         if ( $lookup { !$strfile } ) {