Optimize iteration for Particle
Few weeks ago, Particle was a trend in Japanese Flash guys. Many works were posted to the Wonderfl with a tag “パーティクル祭 (Particle Matsuri)” (means Particle Festival).
In one of them, there is “100,000 particles” posted by bkzen. I think it’s pretty cool! but I need more speed. :p So I posted a quick optmized one. It brings 33% faster (21fps -> 28fps) in my Mac.
You can see what did I changed by clicking “diff” link in above page. In short, I changed an iteration method from Array to LinkedList. Because array index access and calculating and checking an index (variable i) are pretty slow.
// Slow
while (i > num) {
p = particles[i];
// do something
i++;
}
But accessing an member variable is fast because it's optimized by AVM2. So it causes fast iteration.
// Fast
while ((p = p.next) != null) {
// do something
}
Please use this optimization if you need fast iteration especially in case you have a large data.