Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^4: Optimizing with Caching vs. Parallelizing (MCE::Map)

by marioroy (Parson)
on Apr 06, 2020 at 18:19 UTC ( #11115140=note: print w/replies, xml ) Need Help??

in reply to Re^3: Optimizing with Caching vs. Parallelizing (MCE::Map)
in thread Optimizing with Caching vs. Parallelizing (MCE::Map)

Yes, MCE::Map returns the elements in the corresponding order, similarly to map. Actually, there is not much going on for the manager process - very efficient and not to worry about MCE::Map having to preserve order. I compared to MCE::Loop (not ordered) and sometimes MCE::Map is faster other times MCE::Loop. No clear winner. Here is the version using MCE::Loop.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use MCE::Util; use MCE::Loop max_workers => MCE::Util::get_ncpu(); sub collatz { my ($start) = @_; my @seq = $start; push @seq, ($seq[-1] / 2, 3 * $seq[-1] + 1)[$seq[-1] % 2] while $seq[-1] != 1; return @seq } my @sizes = mce_loop_s { my @results; push @results, [$_, scalar collatz($_)] for @{ $_[1] }; MCE->gather(@results); } 1, 1e6; say "@$_" for reverse +(sort { $b->[1] <=> $a->[1] } @sizes)[0 .. 19];
MCE::Map 9.392 seconds MCE::Loop 9.396 seconds

Regards, Mario

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115140]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2021-10-25 11:35 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.