Think about Loose Coupling 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
[download]```

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 about the Monastery: (3)
As of 2024-06-25 02:17 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.