View Full Version : common cpu/mem speed practices
04-14-2011, 05:06 PM
Hi all. Not sure if this is the right forum but here goes. Just starting out in AS3 and i know there are probably a few common practices that everyone knows to use in order to make your app as efficient as possible. For example, just a second ago I learned about vectors and that they are superior to arrays if the situation calls for them. Also, I recently learned variable name length makes a bit of difference. Are there any other common practices everyone knows to do/stay away from?
One specific question I have is: does it slow down your program significantly if you pass references to "big" objects to objects you make a lot of instances of. For example:
I know this is probably a symptom of a bad design pattern (I try to stay as neat/rigid as possible) but I found at some point I had to pass some major object (game logic, sprite container) to instances of a unit that would appear frequently on screen. Does passing references like this slow down your program? Thank you!
04-15-2011, 02:36 AM
Instead of passing things around, try to make objects/classes that have the properties they will need. Compose your design. Don't just throw in things you may need to pass around. But it's a bit of a matter of opinion, composition vs inheritance.
Eh, but not that much; it's better to have something already built, than to have to keep on building it.
Vectors perform better than arrays because you can type cast them, and in some situations what is known as a Linked List (which is a data type you'll have to make yourself) can perform better than even Vectors.
As much as possible try to limit new calls, instantiation is a heavy operation.
Your biggest performance hurdle will be the rendering of graphics.
Try to use the smallest files possible.
...There's more, but those are some basic pointers.
04-15-2011, 02:45 AM
thanks! linked lists would be like objects with a .next reference or something. is there an iterable interface?
04-23-2011, 04:40 AM
Yes, a linked list is an object structure with a next/prev property. They are blazingly fast compared to Array or Vector for certain classes of operations. For example, removing an item from a linked list is a trivial operation that takes constant time regardless of where the item "lives" in the list. An Array or Vector will have worse performance when removing an item from position 0, and the performance hit will be worse the larger the array/vector. Similarly, you can "add" to position 0 of a linked list in the same amount of time as adding anywhere else. Adding to 0 in Vector or Array will require that Flash shifts every other item "up" a location, so just like removing from 0 in a large collection has poor performance in an Array, so will adding to 0 in a larger collection.
If you're going for maximum efficiency then you *won't* use an interface at all. next/prev will be naked public properties, not get/set functions, and you'll manipulate them "by hand".
I don't know if you'd call this a "common" practice, but for certain classes of applications I'd definitely call it a "good" one. Never instantiate if you can help it. This means you should re-use everything that's reasonable to re-use. This means never setting anything to null that you'll have to re-instantiate. It's nice that Flash has a garbage collector, but if you have a high-performance game then you'll *never* want to force the gc to run, or at least you won't want it to run in the middle of gameplay. Wait until the user is at a calm spot like a game-over screen if you're going to let the GC gobble up your trash.
04-30-2011, 04:38 AM
Thanks for all the tips guys. I implemented linked lists today and switched some basic math operations I was using to bitwise operations. Now that I'm using linked lists, when I iterate through them should I use while or for loops? I understand that while loops are slower than for loops but they seem to be the intuitive choice here. Thoughts?
04-30-2011, 09:42 PM
The linked list question seems to be a moot point now. The list seems to be working fine except when I try to remove a child from the camera sprite using a game object retrieved from a linked list, the sprite doesn't recognize it as a display object child. Not sure what I'm doing wrong. Something about references corrupted as a I pass a display object to the list?
vBulletin® v3.8.5, Copyright ©2000-2014, Jelsoft Enterprises Ltd.