Pathologically Eclectic Rubbish Lister PerlMonks

### comment on

 Need Help??

Here is last (hopefully) instalment in this thread; it only concerns faster, compared to the PDL CPAN tutorial, "Game of Life" implementation. But maybe some features used are too advanced for a tutorial. Improvement comes from:

• To achieve wrap around, the ugly glueing in parent node is replaced with dicing along each axis. Very convenient function.
• Horrible expression to compute next generation (requires 8 operations on whole arrays) is replaced with shorter version (just 4).
```use strict;
use warnings;
use feature 'say';
use Time::HiRes 'time';

use PDL;
use PDL::NiceSlice;
use Test::PDL 'eq_pdl';

use constant {
WIDTH  => 20,
HEIGHT => 20,
STEPS  => 1000,
};
my \$x = zeroes long, WIDTH, HEIGHT;

# Put in a simple glider.
\$x(1:3,1:3) .= pdl ( [1,1,1],
[0,0,1],
[0,1,0] );
my \$backup = \$x-> copy;

printf "Game of Life!\nMatrix: %s, %d generations\n",
\$x-> info, STEPS;

# Tutorial
my \$t = time;
for ( 1 .. STEPS ) {
my \$t_ = time;
# Calculate the number of neighbours per cell.
my \$n = \$x->range(ndcoords(\$x)-1,3,"periodic")->reorder(2,3,0,1);
\$n = \$n->sumover->sumover - \$x;

# Calculate the next generation.
\$x = (((\$n == 2) + (\$n == 3))* \$x) + ((\$n == 3) * !\$x);
}
printf "Tutorial: %0.3f s\n", time - \$t;

# Faster
my \$m = \$backup-> copy;
\$t = time;
my \$wrap_w = pdl [ reverse WIDTH - 1, ( 0 .. WIDTH - 1 ), 0 ];
my \$wrap_h = pdl [ reverse HEIGHT - 1, ( 0 .. HEIGHT - 1 ), 0 ];
for ( 1 .. STEPS ) {
my \$n = \$m
-> dice_axis( 0, \$wrap_w )
-> lags( 0, 1, WIDTH )
-> sumover
-> dice_axis( 1, \$wrap_h )
-> lags( 1, 1, HEIGHT )
-> xchg( 0, 1 )
-> sumover;
\$n -= \$m;
\$m = ( \$n == 3 ) | \$m & ( \$n == 2 )
}
printf "Faster:   %0.3f s\n", time - \$t;

die unless eq_pdl( \$x, \$m );

__END__

Game of Life!
Matrix: PDL: Long D [20,20], 1000 generations
Tutorial: 0.341 s
Faster:   0.111 s

Matrix: PDL: Long D [200,200], 100 generations
Tutorial: 0.845 s
Faster:   0.086 s

Matrix: PDL: Long D [1000,1000], 20 generations
Tutorial: 4.422 s
Faster:   0.443 s

Matrix: PDL: Long D [2000,2000], 10 generations
Tutorial: 8.878 s
Faster:   0.872 s

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
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-07-25 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.