Welcome to the Monastery PerlMonks

### comment on

 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

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2021-01-26 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (271 votes). Check out past polls.

Notices?