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

Toby Corkindale toby.corkindale at strategicdata.com.au
Wed Nov 10 20:42:42 PST 2010


On 05/11/10 14:20, Nathan Bailey wrote:
> So the contest requires each turn to be submitted within one second. So
> logically, one would endeavour to use Time::HiRes and an alarm to make
> sure that you submit a turn no matter what happens, but I seem to have
> the syntax for Timer::HiRes/alarm's wrong. I would expect that this:
>     eval {
>        local $SIG{ALRM} = sub { warn "Alarm initiated\n"; print "go\n"; };
>        setitimer(ITIMER_REAL, 0.9);
>        while(1) { ; }
>     };
>     if ($@) {
>        warn "Alarm happened?";
>        die $@;
>     }
> would:
>   a) initiate an alarm event 900ms after the setitimer statement is run
>   b) submit an end turn command ('go') to stdout/the server = successful
> turn completion
>   b) make the $@ block run when it does time out (as it will, with our
> while(1))
>
> So the above code should successfully submit empty turns every turn.
>
> 'Alarm initiated' does get printed, but 'go' doesn't seem to go to the
> server. Am I using alarm wrong or is this a contest-specific issue?


Hi Nathan,
Did you ever establish what was going on here?

Also, I was just wondering - have you actually been having trouble doing 
enough processing inside 1000 ms?

There was a point when I was exceeding it, but I was doing a lot of work 
over and over; It was helpful to create some data structures to hold 
everything at the start of each that I could just reference later.
Also, note that some data won't change at all over the course of the 
game - the location, growth rates, and distances between planets.

I note that the default starter packages are not terribly optimised 
either. For instance, whenever you request a planet by ID, it finds it 
by iterating over the whole lot of planets, and checking to see if the 
ID matches. >.<
A quick optimisation would be to use List::Util's "first", and a better 
optimisation would be to store the planets in a damn hash table.
(List::Util is in core Perl 5.8.8 isn't it?)

I recommend sticking the fleets into a hash table based on destination 
while you're at it..

Cheers,
Toby


More information about the Melbourne-pm mailing list