For the curious, here is a version of TGrep.pm by BrowserUk modified to use MCE. Simply remove or comment out "use threads" if Perl lacks support for threads. Threads is not necessary for MCE::Queue.

package TGrep; use strict; use threads; use MCE; use MCE::Queue; our $WORKERS = 4; sub tgrep(&@) { my $workers = $WORKERS; my $code = shift; my @results; my $Qin = MCE::Queue->new( fast => 1 ); my $Qout = MCE::Queue->new( queue => \@results ); my $mce = MCE->new( max_workers => $workers, user_func => sub { $Qout->enqueue( $code->() ? $_ : () ) while local $_ = $Qin- +>dequeue; } )->spawn; $Qin->enqueue( map{ ref $_[0] ? @{ $_ } : $_ } @_ ); $Qin->enqueue( (undef) x $workers ); $mce->run; return wantarray ? @results : \@results; } sub import { no strict 'refs'; my $pkg = caller; *{ $pkg . '::' . $_ } = *{ $_ } for qw[ tgrep ]; } 1;

Thus, new results emerges.

# $N = 1e6 (4 workers) TGrep....: Took 30.264018774 seconds TGrep MCE: Took 15.292614937 seconds (fast => 0) TGrep MCE: Took 7.824651003 seconds (fast => 1)

I was curious about how MCE::Queue compares to Thread::Queue for the demonstration. The fast option is beneficial for an already populated queue.


In reply to Re: Wanting some clarification / opinions on MCE vs Threads by marioroy
in thread Wanting some clarification / opinions on MCE vs Threads by jmmitc06

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.