Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi choroba,

The three MCE::Map exported functions, mce_map, mce_map_f and mce_map_s all return orderly output (since they mimic, well, map).

(You can also get ordered output from a non-map function using an orderly gather() function such as the ones provided in MCE::Candy.)

You could implement a parallelized version of your code without orderly output just using MCE::Flow:

use warnings; use strict; use feature 'say'; use MCE::Util; use MCE::Flow; 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_flow_s { max_workers => MCE::Util::get_ncpu(), bounds_only => 1, gather => \@sizes, }, sub { my ($mce, $chunk, $chunk_id ) = @_; my ($start, $end) = @$chunk; my @chunk_sizes; push @chunk_sizes, [$_, scalar collatz($_)] for $start .. $end; MCE->gather( @chunk_sizes ); }, 1, 1e6; say "@$_" for reverse +(sort { $b->[1] <=> $a->[1] } @sizes)[0..19];

This runs a tiny bit faster on my system, as you might expect. But it's more code than using MCE::Map, as you can see.


Update: But with a non-ordered input sequence, such as keys %hash, or where processing time for each element is likely to vary, this may be a more significant factor. Also note that you can call gather() multiple times from within the user sub being processed by MCE, without returning, so you can view or handle the output as it is produced (by specifying a callback for your gatherer). And then there's MCE::Stream ... ;-)

$ time perl ch-map.pl 922525 445 922524 445 906175 445 886953 445 615017 447 410011 449 820023 450 820022 450 818943 450 546681 452 970599 458 796095 468 767903 468 511935 470 927003 476 910107 476 704623 504 939497 507 626331 509 837799 525 real 0m4.843s user 0m47.423s sys 0m0.265s

time perl ch-flow.pl 922525 445 922524 445 906175 445 886953 445 615017 447 410011 449 820023 450 820022 450 818943 450 546681 452 970599 458 796095 468 767903 468 511935 470 927003 476 910107 476 704623 504 939497 507 626331 509 837799 525 real 0m4.661s user 0m47.057s sys 0m0.255s

Hope this is of interest!


The way forward always starts with a minimal test.

In reply to Re^4: Optimizing with Caching vs. Parallelizing (MCE::Map) (Updated) by 1nickt
in thread Optimizing with Caching vs. Parallelizing (MCE::Map) by 1nickt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found