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:


In reply to Re: Script Executes Very Slowly by AppleFritter
in thread Script Executes Very Slowly by doubleqq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.