Dan Sugalski wrote:
> I think perhaps a rewrite of life.pasm into perl with some
> benchmarking would be in order before making that judgement.

Following is a rough perl5 version of life.pasm.

On my system [Pentium 166; linux 2.2.18; perl 5.6.1] this takes 96 to 97
seconds; CVS parrot takes 91 to 92 seconds (without JIT)

So, thus far, we aren't doing too badly.  I am sure the perl version can be
optimised, but as it stands I think it is a reasonable equivalent of what
the pasm version does.

--
Peter Gibbs
EmKel Systems

#!/usr/bin/perl
#
# life.perl
#
# Play conway's (no, not *him*. The other conway) game
# of life
#
  # First the generation count
  $I2 = 5000;
  # Note the time (for perl, this is seconds only)
  $N5 = time;
  # If true, we don't print
  $I12 = 1;
  $S15 = "               "
       . "               "
       . "      *        "
       . "      **       "
       . "     * *       "
       . "               "
       . "         *     "
       . "         *     "
       . "         *     "
       . "     *         "
       . "     *         "
       . "     *         "
       . "               "
       . "               "
       . "               ";
  dump_life();

  $I0 = 0;
  while ($I0 < $I2) {
    $I0++;
    generate();
    dump_life();
  }

  $N6 = time;
  $N7 = $N6 - $N5;
  print "$I2 generations in $N7 seconds. ";
  $N8 = $I2;
  $N1 = $N8 / $N7;
  print "$N1 generations/sec\n";

  #GC statistics deleted for perl version


# S15 has the incoming string, S0 is scratch, S1 is scratch, S2 is scratch
#
# I0 is the length of the string
# I1 is the current cell we're checking
# I2 is the count for that cell
# I3 is the offset to the neighbor
sub generate {
  my ($I0, $I1, $I2, $I3);
  $I0 = length($S15);
  $S1 = "";

  for ($I1 = 0; $I1 < $I0; $I1++) {
    $I2 = 0;

    $I3 = (-16 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (-15 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (-14 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (-1 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (1 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (14 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (15 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    $I3 = (16 + $I0 + $I1) % $I0;
    $I2++ if (substr($S15, $I3, 1) eq "*");

    if (substr($S15, $I1, 1) eq "*") {
      if ($I2 < 2 or $I2 > 3) { $S1 .= " "; }
      else { $S1 .= "*"; }
    }
    else {
      if ($I2 == 3) { $S1 .= "*"; }
      else { $S1 .= " "; }
    }
  }

  $S15 = $S1;
}

# S15 has the incoming string, S0 is scratch
sub dump_life {
  return if ($I12 > 0);
  print "\f\n\n\n\n\n\n\n\n\n\n\n";
  print "------------- generation $I0 -------------\n";

  my ($I0, $I1);
  for ($I0 = 0, $I1 = 14; $I1 >= 0; $I1--, $I0 += 15) {
    $S0 = substr($S15, $I0, 15);
    print "$S0\n";
  }

  #sleep 1;
}



Reply via email to