*Disclaimer* This is un-benchmarked, but it's what I usually work to! If anyone 
has any corrections re. speed, I'd be glad to hear them :)

AFAIK, using implicit casting rather than explicit in this case is faster, so 
A1 in your examples is faster than A2. Also, it's better to move your variable 
declaration outside the loop:

var i:int = myArray.length;
// Variable is declared once here and then reused
var myClass:MyClass;
while (i--)
{
   myClass = myArray[i];
}


With vectors, you don't need to cast at all. However, though you can access 
your references directly from the vector using [ ], like you've done in V3, it 
will be faster to store a local reference if you're using it more than once. So 
for example:

var i:int = myVector.length;
var myClass:MyClass;
while (i--)
{
   // Store the reference in a temporary variable to avoid using [ ] 
unnecessarily
   myClass = myVector[i];
   myClass.someMethod();
   myClass.someOtherMethod();
}

will be faster than:

var i:int = myVector.length;
while (i--)
{
   // This uses [ ] twice
   myVector[i].someMethod();
   myVector[i].someOtherMethod();
}


If you're only using the reference once though, it should be faster just to use 
[ ], so:

var i:int = myVector.length;
while (i--)
{
   // Just using the reference once, so not defining a temporary variable:
   myVector[i].someMethod();
}

will be faster than:

var i:int = myVector.length;
var myClass:MyClass;
while (i--)
{
   // This is a waste as we're only using it once
   myClass = myVector[i];
   myClass.someMethod();
}


As I said, I don't have benchmarked figures for these, so don't take it as 
gospel, but I think this was what I read when I read it.

Piers




On 27 Dec 2009, at 00:24, Steven Sacks wrote:

> var instance1:MyClass = new MyClass();
> var instance2:MyClass = new MyClass();
> var instance3:MyClass = new MyClass();
> 
> var myArray:Array = [instance1, instance2, instance3];
> 
> // QUESTION A: CASTING WITH ARRAYS
> 
> var i:int = myArray.length;
> while (i--)
> {
>    // [A1] - Does Flash have to do a look up here?
>    var myClass:MyClass = myArray[i];
> 
>    // [A2] - Does casting it like this boost performance
>    var myClass:MyClass = MyClass(myArray[i]);
> 
>    // [A3] - I know using "as" is slower than direct casting
>    var myClass:MyClass = myArray[i] as MyClass;
> }
> 
> // QUESTION B: CASTING WITH VECTORS
> 
> var myVector:Vector.<MyClass> = Vector.<MyClass>(myArray);
> 
> var i:int = myVector.length;
> 
> while (i--)
> {
>    // [V1] - Will this avoid the lookup?
>    var myClass:MyClass = myVector[i];
> 
>    // [V2] - Or do I need to cast here even though it's a Vector?
>    var myClass:MyClass = MyClass(myVector[i]);
> 
>    // [V3] - And if so, can I do this without a lookup?
>    myVector[i].someMethod();
> 
>    // [V4] - Or do I need to do this?
>    MyClass(myVector[i]).someMethod();
> }
> 
> 
> Thanks in advance for anyone who can shed some light here.  I believe A2/V2 
> is "fastest" but I'm not certain, and I also don't know if it's required for 
> Vector.
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to