the loop (hence the simulation) ran much faster if taxis only had to move one woodlouse, and much slower if it tried to animate fifty at a time.

Huh. Makes sense to me. You're making the program do a lot more work when there's 50 lice, so you have to expect that it will take longer.

What happens when you set up the repeat is that, as soon as the 50ms is up, you start back at the begining of the list of lice (by recursing into the repeat via an update() or DoOneEvent()), even if you never finished moving the later lice. This allows the lice at the begining of the list to keep moving, while the later lice never get to move at all. And, of course, as this process continues ever deeper, you end up with a "deep recursion" error.

You have to face the fact that it will take longer to move all the lice on a slower computer. The computer you're using can apparently move all 50 lice within 50ms but not within 20ms. A slower computer may take 70ms to move them all.

Your original loop (without using repeat) will always move every lice in as little time as possible. You can always slow it down, if you want, but there's no way (except for possibly optimizing the display or movement routines) to make it go faster.

If you want to enforce some maximum frame rate (aka, a minimum amount of time to spend during each refresh), you maybe using something along the lines that benn suggested or, perhaps more simply:

use constant MIN_REFRESH => 0.02; # 20 ms while(1) { my $start = Time::HiRes::time; $taxis->taxis() if $running; DoOneEvent() until Time::HiRes::time-$start > MIN_REFRESH; }

This will run through all the lice each time, without recursion, and will always take at least 20ms to do so, though it may take (much) longer, if it needs to.

bbfu
Black flowers blossom
Fearless on my breath


In reply to Re: Deep recursion in Tk repeat by bbfu
in thread Deep recursion in Tk repeat by polypompholyx

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.