For the kikuchiyo.pl script, the fix is initializing subdircount to maxforks - 1. That ensures safety, as each worker sets $i using the same range (starting at 0).

my $subdircount = $maxforks - 1;

Update: Added results for threads and MCE::Child.

Running with 512 workers:

######################################################## # threads, mod: init data after spawning threads # https://www.perlmonks.org/?node_id=11149880 ######################################################## $ rm -fr temp data.dat $ time ../threads.pl ; ls -lh data.dat Parsing 61441 files maxthreads: 512 regex: 13.389084 real 0m13.443s user 3m25.771s sys 0m3.514s -rw-r--r-- 1 mario mario 471M Jan 28 13:25 data.dat ######################################################## # kikuchiyo.pl, mod: my $subdircount = $maxforks - 1; # https://www.perlmonks.org/?node_id=11149910 ######################################################## $ rm -fr temp data.dat $ time ../kikuchiyo.pl ; ls -lh data.dat Parsing 61441 files maxforks: 512 regex: 7.810577 real 0m9.393s user 2m11.069s sys 0m4.456s -rw-r--r-- 1 mario mario 471M Jan 28 12:46 data.dat ######################################################## # MCE::Child, mod: init data after spawning processes # https://www.perlmonks.org/?node_id=11149917 ######################################################## $ rm -fr temp data.dat $ time ../mcechild.pl ; ls -lh data.dat Parsing 61441 files maxforks: 512 regex: 7.858324 real 0m7.890s user 2m11.238s sys 0m4.026s -rw-r--r-- 1 mario mario 471M Jan 28 13:31 data.dat ######################################################## # mceiter1.pl, MCE chunking # https://www.perlmonks.org/?node_id=11149971 ######################################################## $ rm -fr temp data.dat $ time ../mceiter1.pl ; ls -lh data.dat Parsing 61441 files maxforks: 512 chunksize: 128 regex: 7.827401 real 0m7.885s user 2m12.198s sys 0m2.232s -rw-r--r-- 1 mario mario 471M Jan 28 12:50 data.dat ######################################################## # mceiter2.pl, MCE chunking + workers appending data.dat # https://www.perlmonks.org/?node_id=11149978 ######################################################## $ rm -fr temp data.dat $ time ../mceiter2.pl ; ls -lh data.dat Parsing 61441 files maxforks: 512 chunksize: 50 regex: 4.294076 real 0m4.328s user 2m2.587s sys 0m1.759s -rw-r--r-- 1 mario mario 471M Jan 28 12:51 data.dat

For cpu-bound task, there's no reason to run many-many workers; beyond physical limitation.

$ time ../threads.pl Parsing 61441 files maxthreads: 32 regex: 10.622881 real 0m10.649s user 3m35.313s sys 0m2.173s $ time ../kikuchiyo.pl Parsing 61441 files maxforks: 32 regex: 7.71529 real 0m8.276s user 2m7.270s sys 0m2.150s $ time ../mcechild.pl Parsing 61441 files maxforks: 32 regex: 7.859328 real 0m7.889s user 2m4.981s sys 0m2.286s $ time ../mceiter1.pl Parsing 61441 files maxforks: 32 chunksize: 128 regex: 7.545659 real 0m7.581s user 2m7.077s sys 0m1.775s $ time ../mceiter2.pl Parsing 61441 files maxforks: 32 chunksize: 50 regex: 4.066287 real 0m4.100s user 2m3.994s sys 0m0.880s


In reply to Re^7: Script exponentially slower as number of files to process increases by marioroy
in thread Script exponentially slower as number of files to process increases by xnous

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.