Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2020-09-21 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (126 votes). Check out past polls.

    Notices?