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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi choroba and 1nickt,

I wonder about choroba's example involving several array fetches inside the loop. The following attempts to find out.

Diff output

$ diff choroba.pl 1nickt.pl 8,9c8,11 < push @seq, ( $seq[-1] / 2, 3 * $seq[-1] + 1 )[ $seq[-1] % 2 ] < while $seq[-1] != 1; --- > while ( $n != 1 ) { > $n = $n % 2 ? 3 * $n + 1 : $n / 2; > push @seq, $n; > }

Demo choroba.pl

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

Demo 1nickt.pl

use warnings; use strict; use feature 'say'; use MCE::Flow; sub collatz { my ($n) = @_; my @seq = $n; while ( $n != 1 ) { $n = $n % 2 ? 3 * $n + 1 : $n / 2; push @seq, $n; } return @seq; } my @sizes; mce_flow_s { max_workers => MCE::Util::get_ncpu(), bounds_only => 1, gather => \@sizes, }, sub { my ($start, $end) = @{ $_[1] }; my @chunk_sizes; push @chunk_sizes, [ $_, scalar collatz($_) ] for $start .. $end; MCE->gather( @chunk_sizes ); }, 1, 1e6; MCE::Flow->finish; say "@$_" for reverse +(sort { $b->[1] <=> $a->[1] } @sizes)[0..19];

Run time on an AMD 3rd Gen Ryzen Threadripper 3970x box - SMT disabled

max_workers => 1 choroba 21.432 seconds 1nickt 18.644 seconds max_workers => 2 choroba 10.808 seconds 1nickt 9.348 seconds max_workers => 4 choroba 5.836 seconds 1nickt 4.992 seconds max_workers => 8 choroba 3.163 seconds 1nickt 2.731 seconds max_workers => 16 choroba 1.835 seconds 1nickt 1.623 seconds max_workers => 32 choroba 1.218 seconds 1nickt 1.105 seconds

Interestingly, the more cores the lesser the difference on this hardware.

Regards, Mario


In reply to Re^5: Optimizing with Caching vs. Parallelizing (MCE::Map) (Updated) by marioroy
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 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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-11-28 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?