1999 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Author had a Free File of the Month
  • Won a Competition
  • Bought between 10 and 49 items
+4 more
bitfade says

I’m posting this coz i’ve read a lot of threads about flash code optimizations like bitwise math, fast alternatives to slow Math.* functions, callbacks vs events and so on.

well, here’s the truth about this kind of optimizations: 99.99% they are just useless.

AS3 (AVM2) is fast, way faster then you could ever need in most real world applications.

plus, flex/flash compilers su**s…...

they will never optimize your code, basicly this means that you could write something simple like a+b+c in thousands of different ways and the compiler will always produce a different bytecode.

so, when you try to optimize math, you’ll end up writing the same thing again and again searching for which one produces a better (faster) compiler code.

but, again, this is useless for almost any item here on AD

my current work involve moving 2500 3D particles at 25fps, this means something like 20-30 float multiplications for each particle/iteration.

well, if total project cpu usage is 100, all this math stuff only requires 5-6.

First thing you’ll need to optimize for getting better performances is the display list: reduce number of objects, simplify complex vector mcs (or convert them to bitmapdata) and most important: only use scaling and filter when really necessary.

a single filter can eat like 20/25% of your total project cpu requirements.

Also, reuse object: do not create Points, Rectangles or other simple objects each time you need them.

Each time you use the “new” operator, a new istance has to be destroyed at some point: the Garbage Collector will take care but this doesn’t comes free.

Another thing you should know is that memory access is way slower than math.
Array are slow, untyped objects slower.

Things are better now with Vectors but still if you do a lot of read/write operations, you may get faster code by reducing them.

1955 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Netherlands
  • Referred between 10 and 49 users
  • Sold between 5 000 and 10 000 dollars
Emroni says

What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?

Understand the reusing of objects part, thats a really good one, especially when working with 3D objects.

Agree with the Math not being such a big problem. I mean, if its 1 ms faster one way than the other, it is nothing compared to using a filter on an object that consumes percentages of cpu.

4140 posts
  • Bought between 1 and 9 items
  • Brazil
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
tsafi says

Well I don’t know about converting complex vector to bitmapdata as long as your bitmap is static as a holder but it take a lot if you don’t use it in the right way and it’s a tricky subject . Graphic/shape class are lighter but they don’t carry the Sprite syntax function .

I agree filters are big enemy when it comes to Cpu. And a reuse objects is great with class less with time line base

Let me add any kind of for loop the less you use the faster things will move for you. Removing for loop is a pain subject on large projects like templates.

Also Tween engine….. well the list goes on … :p

And why use the useless word, its not :P

1999 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Author had a Free File of the Month
  • Won a Competition
  • Bought between 10 and 49 items
+4 more
bitfade says
What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?
according to flex profiler: if you call BitmapData.dispose() and then set to undefined all references, memory is freed almost instantly

2671 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 7-8 years
  • Referred between 100 and 199 users
  • Sold between 100 000 and 250 000 dollars
  • United Kingdom
  • Won a Competition
Chuckanucka says

great post! thanks.

1999 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Author had a Free File of the Month
  • Won a Competition
  • Bought between 10 and 49 items
+4 more
bitfade says
And why use the useless word, its not :P
well, i didn’t said they are 100% useless :P
4140 posts
  • Bought between 1 and 9 items
  • Brazil
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
tsafi says
And why use the useless word, its not :P
well, i didn’t said they are 100% useless :P

Ti perdono :P

5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says

Great tips :) Why is scaling bad?

1955 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Netherlands
  • Referred between 10 and 49 users
  • Sold between 5 000 and 10 000 dollars
Emroni says
according to flex profiler: if you call BitmapData.dispose() and then set to undefined all references, memory is freed almost instantly

Got it. Then I have the following scenario:

- Load image with Loader class
- Create ‘bmp’ variable, a Bitmap
- Place image in bmp with ‘bmp = Bitmap(loader.content)’

Now we have both the image in the loader as well as the image in the bmp variable. Can we delete the image in the loader without losing the image in the bmp? And how?

I always use ‘loader = null’, but dont have the idea that that works. I also heard that dispose() wont work if there is still a reference (the bmp in this case) to that image. Thoughts?

5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says
What about setting variables to null or NaN after usage? Does that have any improvement in your opinion?

If the life-cycle of your object has ended, destroying it will free memory and improve performance. But only when the GC decides to run a sweep. Not sure about BitmapData. I always thought thesame principle applied, but if it frees memory immediately that’s awesome.

by
by
by
by
by
by