-
hippie hater
[RESOLVED] variables in loops
I've heard somewhere that when you will declare a loop, or function, that will be callled many times, it will run faster if you create the variables it will use outside of him, so you dont have to remake them all the time, however when the stuff grows that tends to pollutes the code outside, so, for those of you who had finish something, does it really worth?
Last edited by Cimmerian; 02-22-2008 at 04:21 AM.
-
Senior Member
In AS3 you should do that anyway so you can set them to private or public.
The speed difference isn't actually that great unless it is a very critical function or loop that gets called very often.
It's a good practice to use but don't expect it to do anything to your fps
-
In AS3 you should do that anyway so you can set them to private or public.
Errr.... no you don't. Local variables are still very useful. For loop counters especially (which this thread is about). You don't want those hanging around in memory when they're not used anymore. And what does that have to do with AS3? You can use private/public in AS2 just as easily (well, there are differences because these modifiers are only checked at compile time, but still).
As for the question : it makes only a little difference, but in games all little bits count. I wouldn't really do it in things that are called only once in a while, but for main loop stuff, it definately doesn't hurt.
-
AS3 employs a method called variable hoisting in which variable declarations are "hoisted" to the top of their scope- this means that declaration in or out of a loop in the context of a function produce the same compiled result.
-
Good to know. I read in an article somewhere that it did matter in AS3, and spent some time "optimizing" an engine of mine with this... but I guess I shouldn't have bothered then
-
hippie hater
In the case of functions what i mean is something like move up the scope, i mean, for example, instead of this:
Code:
//inside someClass
public function calculateSomeAngle(p1:Point, p2:Point):Number{
var angle:Number = 0;
//calculate whatever and in the end store in angle
return(angle);
this:
Code:
var angle:Number = 0;
public function calculateSomeAngle(p1:Point, p2:Point):Number{
//change the var angle from outside and return it
return(angle);
assuming you use that function each frame so it would not create a new var angle every time its called
-
If what newblack is saying is true, I guess it wouldn't make a difference in as3. In as2 it would make a difference, but whether it's worth it or not depends on your needs.
-
hippie hater
Well, thanks everyone, i've decided i go for the clean way.I doubt that that little speed you get makes some difference in AS3, anyway
-
Professional Flash Developer
I don't know about speed advantages (per se), but I can see some garbage collection advantages with class scope loop vars over local (method) scope loop iteration vars. The GC doesn't have to mark sand sweep as many objects repeatedly if you don't have a lot of local declared vars hanging around to get rid of. This is mostly the case with loop iterators that are created on every frame tick for things like collision detection, AI, etc. Also, if you are doing copyPixels operations, all of those Point and Rectangle objects gunk memory and then wait around to be cleaned up.
On the other-hand, your memory foot print overall is higher for a longer duration if you rely on global class score vars for loop iterators, and object instances. In some cases, that might be worse than leaving the GC with a lot of stuff to cleanup on every frame tick.
-
Originally Posted by Fall_X
If what newblack is saying is true, I guess it wouldn't make a difference in as3. In as2 it would make a difference, but whether it's worth it or not depends on your needs.
class members exist in the scope of that class. when dealing with variables local to a method, declaration happens per invocation. in most cases, the class member implementation should outperform variables in the local scope of a function.
so just to summarize and not leave this one aspect open-ended: variable "hoisting" happens within the scope of a function.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|