Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Timers vs Infinite Loops

Bloodsong Termagant
Manic Artist
Join date: 22 Jan 2007
Posts: 615
10-10-2007 08:55
heyas;

making a new thing with flapping wings -- very simple wings, they just flap at one constant rate, never stop, never change.

so im thinking, geeze, constantly calling a timer every0.4 seconds seems like a lot of system stress. what if i do a loop of ten flaps and only call it every 4 seconds?

and then i thought, well, heck, why don't i just throw out the timer altogether and put in an infinite flapping loop?


obviously, im not a real programmer. is there a reason not to? i mean, if it's spaced with llSleep calls so it doesn't overrun the command buffer. would touch events and listen events fail to fire while the loop was going on? would the world end? would the grid crash? would we be plunged into an alternate dimension where everyone grows old and we only get paid every other week?


on the more philosophical side (er, okay, can't get more philosophical than that, how about on the more techno-geek side?).... IS there any difference in running a one command in a timer event every 0.4 seconds and running a loop of ten commands every 4.0 seconds? or is that just an illusion in my mind?
_____________________
Why Johnny Can't Rotate:
http://forums.secondlife.com/showthread.php?t=94705
Malachi Petunia
Gentle Miscreant
Join date: 21 Sep 2003
Posts: 3,414
10-10-2007 09:17
From: someone
obviously, im not a real programmer. is there a reason not to? i mean, if it's spaced with llSleep calls so it doesn't overrun the command buffer. would touch events and listen events fail to fire while the loop was going on? would the world end?
Anytime you are in a function or event handler, all other events (touch, listen, etc.) are blocked, queued, and then thrown away upon queue full. If you want any event handling at all, infinite loops are Really Bad.

llSleep blocks also, like any other API call; it is nothing like unix sleep(2).

There is some part of me that thinks 400ms per flap is a little on the expensive side for causing client updates, but I'm not real certain about that. I'd try 1s timers and see if there is noticable difference.
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
10-10-2007 10:20
I doubt there's much difference in the overhead between llSleep() and a timer. Pretty much all the same stuff has to happen. The main difference is that for llSleep(), the task stack isn't empty, whereas for timer the event handler is called rather than continuing the task from where it slept. Probably not much difference. Actually, the timer might be more efficient since it might be optimized to run periodically, rather than creating a wakeup and deleting it each time.

As mentioned above, there's a disadvantage to using a loop: the object can't react to any events. I recommend using the timer, allowing you much more freedom to add features and probably not affecting the efficiency significantly.
Bloodsong Termagant
Manic Artist
Join date: 22 Jan 2007
Posts: 615
10-12-2007 07:33
great!

thanks, guys :)
Scott Tureaud
market base?
Join date: 7 Jun 2007
Posts: 224
10-12-2007 08:01
there's a hell of a lot of difference between using an infinite loop and a timer.

the infinite loop literally ends up eating 2% of the script resources of the sim, compared to .02-.05% of the resources with a timer.
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
10-12-2007 08:36
Scott, can you back that up with more specific information? What resources are you talking about? How would this be measured?

Regards,
Jeff
Scott Tureaud
market base?
Join date: 7 Jun 2007
Posts: 224
10-12-2007 09:51
quite simple I got a sim owner to measure resource usage of a few scripts. two of them were fish tails that I was working on the first one that used a simple infinate loop with the sleep command end up using 2% of the sims resources. then I rescripted the same exact script using a timer and no sleep function and it was down to .05% of script resource usage on the sim.
poopmaster Oh
The Best Person On Earth
Join date: 9 Mar 2007
Posts: 917
10-12-2007 10:53
so 50 fish with wiggly tails using the loop method would use up 100% of the sim resources?
Scott Tureaud
market base?
Join date: 7 Jun 2007
Posts: 224
10-12-2007 11:08
not quite the sim seperates it resources into physics, scripting, and a few other things. so by throwing 50 of my wiggly fishy tails I'd fill up the pool of resources for scripts.

so then scripts would start acting goofy. AO's, merchants, and all those goodies.
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
10-12-2007 11:15
Are you sure?

If you can, I'd like to see the 50 fish tail experiment done. I wouldn't trust the resources readout to be linear with the number of copies of an object running.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Scott Tureaud
market base?
Join date: 7 Jun 2007
Posts: 224
10-12-2007 11:28
it most likely isn't linear. but lineariness is a nice simplification. and the pools are in all likeliness not a fixed size either based on what the sim is running.
Wildefire Walcott
Heartbreaking
Join date: 8 Nov 2005
Posts: 2,156
10-12-2007 11:31
From: Scott Tureaud
there's a hell of a lot of difference between using an infinite loop and a timer.

the infinite loop literally ends up eating 2% of the script resources of the sim, compared to .02-.05% of the resources with a timer.

I can back this up. I wrote a sim uptime monitor to IM me when my sims restart and initially used an infinite loop with a 15-second llSleep. It was always among my top 10 script resource hogs in any island I put it in. Now that I've converted it to use timers it doesn't even show up in the top 100.
_____________________
Desperation Isle Estates: Great prices, great neighbors, great service!
http://desperationisle.blogspot.com/

New Desperation Isle: The prettiest BDSM Playground and Fetish Mall in SL!
http://desperationisle.com/

Desperation Isle Productions: Skyboxes for lots (and budgets) of all sizes!
Wildefire Walcott
Heartbreaking
Join date: 8 Nov 2005
Posts: 2,156
10-12-2007 11:32
From: poopmaster Oh
so 50 fish with wiggly tails using the loop method would use up 100% of the sim resources?

No, a sim automatically scales back on script performance when things get dire.
_____________________
Desperation Isle Estates: Great prices, great neighbors, great service!
http://desperationisle.blogspot.com/

New Desperation Isle: The prettiest BDSM Playground and Fetish Mall in SL!
http://desperationisle.com/

Desperation Isle Productions: Skyboxes for lots (and budgets) of all sizes!
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
10-12-2007 12:19
Yes, Thirded. llSleep is a terrible lag inducer as i've seen from my own experience. Use sparingly. :)
_____________________
Incony Hathaway
Registered User
Join date: 18 Feb 2007
Posts: 235
10-12-2007 12:58
"Use sparingly"

hmm.. where can i find some sparingly......:)
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
10-12-2007 13:01
to put in simpler terms, don't use it a lot. better? :p
_____________________