View Full Version : Is it faster to iterate through BitmapData compared to Array?
Glidias
August 5th, 2007, 09:50 AM
well, what do you think?
Is it faster to iterate through BitmapData's width and height to gather values as compared to iterating through an Array and getting array[i] which links to some ClassObject?
In the sense, rather than getting the values through Array[i].somePropertyOfObject....., you get the values through getPixelAt(1,4), where 1 is the index and 4 is some property of the object that is known/documented by the programmer. Of course, you are limited to only integers from 0 to a certain amount (depending on whether you have transparency enabled as well)...but how much more performance can you squeeze out of this, especially when you have a 800 of such particles to keep track of? Of course, having to ensure integer (int) of thingsl, might be quite a headache in terms of free-style programming, as you might not get the exact precision required.
Your thoughts...Just wondering if there's a performance boost, and whether it's worth the trouble..
Sirisian
August 5th, 2007, 08:42 PM
You'd have to test it. As far as I know it probably wouldn't be faster. Also try using the for each(var i:someObject in ArrayFoo){i.someProperty} that might be faster.
Rezmason
August 5th, 2007, 09:47 PM
If you're looking for speed, I recommend using a linked list. Basically, you create a Node class that stores a value and a reference to another Node instance. These act as links in a chain; you start with the first link and iterating through the chain until you've run out of links. I've found this to be much, much faster than Arrays, but only because the data I was managing was large. The advantage may be negligible for small amounts of data.
rjewson
August 6th, 2007, 04:23 AM
One option (and it has several drawbacks) is to just use the display list.
Anything that inherits from DisplayObjectContainer gives you this functionality. Its a linked list, you can add and remove items anywhere. Your node just has to inherent from display object.
I see you mentioned particles in the original post. I just did exactly the same thing using my own custom particle/linked list, each Particle being a subclass of bitmap. It was pretty fast - iterating over 2000 particles and updating velocity vectors & alpha channels at 30fps took almost no time - however rendering them ate my cpu, I'll post the code/demo later today.
Sirisian
August 6th, 2007, 04:56 AM
rendering is your enemy. You can render tons of small images or a few large ones. There are tricks to rendering and such like bitmapData rendering. If you become really bored you can render around 20,000+ pixel particles fairly easily and fast.
http://www.sirisian.templarian.com/flash/flashIsoWave/flashIsoWave.html
that's like 16384 pixels rendered with velocity and interpolation. What's cool though is that even with color transforms flash can pull off some very fast rendering:
http://www.sirisian.templarian.com/flash/pg/pg.html
click and notice the difference between rendering 500 particles at different sizes with color transformations and alpha channels. Albeit that example is a tad slow because of how implemented some of the code.
Can't wait to see rjewson particle system. My new one is built into my engine so I'll release that in like a year. :)
rjewson
August 6th, 2007, 05:24 AM
Nice - I like the wave demo.
This http://www.box.net/shared/45l5lsiyui runs at 20fps.
F1 - F3 trigger effect
Space - toggle visibility of new particles
Left/Right arrow - apply wind to fires
This uses 2 linked lists one to mange the particle cache one the active particle list. I'll might try and modify it to use the standard AS Array to see how much slower it is. If you just need to iterate over a list then I doubt it will be much slower, however if you need to add, remove, splice entries then go with the linked list.
This demo uses a individual bitmap per particle, each rendered independently. As Sirisian (http://www.kirupa.com/forum/member.php?u=27929) mentioned, rendering is your enemy. I could have speeded this up by rendering each emitter to a single bitmap bring the bitmap count down from 2000 to about 20 or 30. I might try that as well.
Hit space to turn of the visibility of new particles - this should give you the rendering overhead. The rest of the cpu is the looping & physics calculations. Actually I'll make a switch for that as well.
senocular
August 6th, 2007, 08:36 AM
Here is the thread that covers the use of linked lists over arrays (or objects or Dictionaries)
http://www.kirupa.com/forum/showthread.php?t=268557
Rezmason
August 6th, 2007, 11:38 AM
That was a good thread. Good times.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.