Is it quick to reset the property set or create a new instance?
I have Matrix
one that I recycle and use to draw instances DisplayObject
onto Bitmap
.
At the moment I am resetting Matrix
before I create each item, for example:
_matrix.a = 1;
_matrix.b = 0;
_matrix.c = 0;
_matrix.d = 1;
_matrix.tx = 0;
_matrix.ty = 0;
Would it be better to do the above or just do it?
_matrix = new Matrix();
In general, I would say the first one, however I'm not sure if Matrix
there is heavy work going on in the case for each of these properties, then I reset (mathematically).
source to share
I think that reusing the same instance is Matrix
more efficient than creating a new one every time.
In fact, creating a new instance is relatively cumbersome and why caches are used: create multiple instances and reuse them instead of creating a large number of instances.
I'm running a little test and it confirms my answer:
var t:Number;
var i:int;
var N:int = 10000000;
t = getTimer();
for (i = 0; i < N; i++) {
_matrix = new Matrix();
}
trace(getTimer()-t); // 7600
t = getTimer();
for (i = 0; i < N; i++) {
_matrix.a = 1;
_matrix.b = 0;
_matrix.c = 0;
_matrix.d = 1;
_matrix.tx = 0;
_matrix.ty = 0;
}
trace(getTimer()-t); // 4162
Finally, note that the difference is not that big and that creating 10000000
new instances only takes place 7600 ms
, so unless you create thousands of matrices per frame, either approach will have no discernible performance impact.
EDIT:
Using the method identity
will have the advantages of both approaches (simplicity and performance):
t = getTimer();
for (i = 0; i < N; i++) {
_matrix.identity();
}
trace(getTimer()-t); // 4140
source to share