in reply to Script Executes Very Slowly

Howdy doubleqq, welcome back to the Monastery!

This is my code that works but runs slow:

Are you sure that's your actual code, your working code? I'm asking because of this:

print RESULT “Company"."\t”.”Name"."\t”.”Title"."\n";

and this:

if (($match[0] =~/\Q$sTab[1]\E/) and (define $match[1])){

and aren't valid quoting characters in Perl, and define isn't a (built-in) function: you most likely meant defined there, but the uncaught typo makes me wonder.

That said, here's two tips:

In your code, you're not just needlessly reprocessing your list of names and titles, you're doing so for every single last line read from your $longList. Don't do that; preprocess it into an efficient data structure, e.g. a hash, and do so outside the main loop. Better yet, have whatever generates this list generate a hash in the first place, if that is an option.

For example, take the following:

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; # I'm assuming you can't control how @flab is populated. my @flab; push @flab, "name_$_\ttitle_$_" for (1..200); # turn @flab into a hash my %titles = (); foreach (@flab) { my ($name, $title) = split /\t/; $titles{$name} = $title; } open my $LONG_LIST, "<", "longlist.txt" or die "Cannot open file: $!"; say "Company\tName\tTitle"; while (<$LONG_LIST>) { chomp; my ($company, $name) = split /\t/;; say "$company\t$name\t$titles{$name}" if($titles{$name}); }

To test this, I generated a longlist.txt file as follows:

$ for i in `seq 1 72000`; do echo -e company_${i}\\tname_${i} >>longli +st.txt ; done

The above script then runs in 0.122s (wallclock time) on my system, compared to about 13s for the version you posted above, a speedup of a factor of more than 100. With bigger @flabs, you'll see even more of a speedup.

Other notes: