sub lcssw { my ($s1, $s2) = @_; my $i; my %codes; my %words; for ($s1, $s2) { $_ = join '', map { $codes{$_} = chr(++$i) if !exists($codes{$_}); $codes{$_} } $_ =~ /\w+/g; } my $lcss = lcss($s1, $s2); $lcss = "" if (!defined $lcss); @words{values %codes} = keys %codes; return join ' ', @words{ $lcss =~ /./sg }; }
Well, whatever you do, you should start right here. You call this function 300000000 times, and it does a lot of things, . Does it actually increase performance (compared to just using lcss on strings?) I mean, it might, but it allocates hashes and strings, accesses keys and values in hashes, runs the regex engine...

Also, while your lcssw could be improved (for example, return '' if not defined $lcss), real optimizations depend on your data. Are there many strings that won't match? You could try to filter them out using some simpler algorithm, and then run the full version on others. How gigantic are your strings? If not too big, you can preprocess them (that is, do most of the work that lcssw does, but just once per array - at least for the smaller array).

The whole thing looks pretty parallelizable, threads or forks could help with using more CPUs to process your arrays.

Anyway, without some representative examples of your data it's hard to tell, so how about posting some.


In reply to Re: Improving script's speed and performance... by Anonymous Monk
in thread Improving script's speed and performance... by Anonymous Monk

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.