View Full Version : Math function calls vs. Tables
dblOprimate
01-16-2011, 12:22 PM
I have a program that uses Math.sin, and Math.random, quite a bit. As it stands I have about 600 independent MovieClips which are constantly changing in some way or another. Any more than that my laptop begins to glow and my desk catches fire. Is it worth it to make tables to call from instead of using the Math functions all the time, or is the processing power being eaten up in all the redrawing.
zyxstand
01-16-2011, 03:24 PM
uhm... 600 independent mc's? it's probably the drawing that will cause most of your problems. maybe if you share what you are doign we could help you optimize it. also, in regards to caching sin and cos functions, i would say do it and tell us whether it changed anything! if you're doing, for example, snow flakes on the screen, one thing you could try is make a movieclip that holds 30 snow flakes and add 20 of those to the stage. with such a high number, you won't see that some move the same way as others.
dblOprimate
01-16-2011, 09:36 PM
I'm working on a program that is like interactive scenery. Started with some grass, moved to some clouds and my lil' CPU says it'll work about 600 mc's after that it get's starts to get really slugish. Take a look if you want, but I'll warn you it's a jungle of code in there. With the program itself, just ignore the crappy halfway done menu screen and click the black box in the bottom right corner. Dragging across the screen will create wind which makes everything move.
-The math for the wind isn't quite right yet, so the clouds don't move quite right... yet.
Thanx for taking a look.
zyxstand
01-17-2011, 04:28 PM
yes it does make the cpu work quite a bit. why do you need to calculate these things? why can't you just make pre-rendered movieclips of swaying grass? btw, i can't open cs5 ;P
maskedMan
01-17-2011, 07:46 PM
A table lookup should always be faster than a function call. If you can assuredly pre-calculate very specific values, and you know you won't have to deviate from them (eg: find values in between the ones you've caluculated) then this should save you time and cycles.
But yeah, 600 mcs is probably much more problematic than 600 calls to Math.sin/Math.cos
dblOprimate
01-18-2011, 10:32 AM
Cool.. thank you.
I can make a table to replace the functions. I just didn't want to spend the time if the gain was negligible or none at all.
It's a lot of moving around multiple instances of the same object to create a psuedo natural pattern.
zyxstand
01-18-2011, 04:49 PM
is it really? for instance, it seems that blades of grass that are at the same x position will move the same direction. so technically, you can move them at the same time with the same swing. if you're throwing in some variability or randomness even among blades that are at the same x, then you can just group a few of those together along the same x.
so, why can't you just make movie clips? it seems that maybe you will change things, like strength of breeze and whatnot, but even so you should try to use prerendered movieclips of grass, rather than calculating (or even pre-rendering) the bend-amount. same goes for whatever else you're planning on moving. it may be fine for now, but when you add a few hundred more and add other effects, you're gonna need more optimizations besides pre-rendered sin/cos.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.