[Melbourne-pm] Timer::HiRes and alarms? (Was: AI Contest)

Toby Corkindale toby.corkindale at strategicdata.com.au
Wed Nov 10 21:32:50 PST 2010


On 11/11/10 15:56, Kim Hawtin wrote:
> Now it only times out when theres more than 100 or so of my fleets
> on the move and that seems to be the parsing code ...

BTW, did you precompile and move the regexes out of the parsing loop? I 
*think* that improved performance, but I've been slack and am just using 
Time::HiRes rather than NYTProf, so maybe just ignore me :)

diff --git a/PlanetWars.pm b/PlanetWars.pm
index 24592c8..e8c1064 100644
--- a/PlanetWars.pm
+++ b/PlanetWars.pm
@@ -113,6 +113,10 @@ sub IsAlive {
      }
  }

+# Cache regex's between runs..
+my $planet_r = qr/^\s*P\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/;
+my $fleet_r = qr/^\s*F\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/;
+
  sub ParseGameState {
      my ($self, $gameState) = @_;
      my $planet_id = 0;
@@ -121,13 +125,13 @@ sub ParseGameState {
      foreach (@$gameState) {
          next if /\s*#/; # Skip comments.

-        if ($_ =~ m/^\s*P\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {;
+        if ($_ =~ $planet_r) {
              push(
                  @{$self->{_planets}},
                  new Planet($planet_id,$1,$2,$3,$4,$5)
              );
              $planet_id++;
-        } elsif ($_ =~ 
m/^\s*F\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {
+        } elsif ($_ =~ $fleet_r) {
              push(
                  @{$self->{_fleets}},
                  new Fleet($fleet_id,$1,$2,$3,$4,$5,$6)


More information about the Melbourne-pm mailing list