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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |