PDA

View Full Version : Do you cast?


cpucpu
04-18-2010, 06:14 AM
In spite of that i want to improve my coding style and better practices, this is something i just haven't got used to.
I someone would require me to start to cast things, i think i'd start to cast every variable that's supposed to contain any value of any tipe at any located place, just because i don't really know when to use it or what are its real world advantages.

I just use cast at places where i'm obligated to (a.k.a. when the compiler throws an error, i don't let me go on).

I have heard some things, as that 'cast()' is faster than 'as', and that cast in to vector values actually decreases its performace. Don't know what you think.

jonnyleeharris
04-18-2010, 12:53 PM
In spite of that i want to improve my coding style and better practices, this is something i just haven't got used to.
I someone would require me to start to cast things, i think i'd start to cast every variable that's supposed to contain any value of any tipe at any located place, just because i don't really know when to use it or what are its real world advantages.

I just use cast at places where i'm obligated to (a.k.a. when the compiler throws an error, i don't let me go on).

I have heard some things, as that 'cast()' is faster than 'as', and that cast in to vector values actually decreases its performace. Don't know what you think.

It will come naturally as experience kicks in. When and why to cast, whether or not the cast is because you need it, or because of some more profound architectural problem etc.

The Cast(theVariable) method IS faster. Just don't try to do it for Arrays. See why here: http://www.humblecode.com/casting-to-array-in-actionscript-3-0-a-word-of-warning/

Good luck.

maskedMan
04-18-2010, 03:43 PM
The Cast(theVariable) method IS faster.


That's funny, I found just the opposite just now.


import flash.display.*;
import flash.text.*;
import flash.utils.getTimer;

var i:int;
var ilen:int;
var time:int;

var txt:TextField = new TextField();
txt.autoSize = "left";
txt.background = true;
txt.border = true;
txt.defaultTextFormat = new TextFormat("Courier New");
this.addChild(txt);

var mc:MovieClip = new MovieClip();
var s:Sprite;

time = getTimer();
for(i = 0 ; i < 1000000 ; i++){
s = mc as Sprite;
}
time = getTimer() - time;
txt.text = "as upcast\t" + time.toString() + "ms\n";

time = getTimer();
for(i = 0 ; i < 1000000 ; i++){
s = Sprite(mc);
}
time = getTimer() - time;
txt.appendText("coersion up\t" + time.toString() + "ms\n");



s = new MovieClip();
mc = null;

time = getTimer();
for(i = 0 ; i < 1000000 ; i++){
mc = s as MovieClip;
}
time = getTimer() - time;
txt.appendText("as downcast\t" + time.toString() + "ms\n");

time = getTimer();
for(i = 0 ; i < 1000000 ; i++){
mc = MovieClip(s);
}
time = getTimer() - time;
txt.appendText("*coersion down\t" + time.toString() + "ms\n ");
txt.appendText("\n\n * Coersion downcast results in runtime error if failure. \n As simply returns null ");




In my browser, it's showing Cast() to be twice as slow.




as upcast 58ms
coersion up 115ms
as downcast 48ms
*coersion down 95ms


* Coersion downcast results in runtime error if failure.
As simply returns null

TomMalufe
04-19-2010, 02:10 PM
Either way, you should always type cast your variables on declaration. Other times you may have to cast a variable like maskedMan is talking about as well (for example in event handlers). But it's always best practice to cast your variables when possible. Leaving it up to the compiler to figure out what a variable's type is will almost always be slower and could cause errors.

thenine
07-09-2010, 07:17 AM
i cast quite alot. Usually a base class to a specific class.

I get a little wary of peformance issues using cast or ints or uints and so on. Its all meaningless compared to your rendering. Concentrate on the display list and optimising that and performance will be fine.