Hi piers,
I am trying to follow your topic.
What is in your TestClass.as??
Kind regards
Cor
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Piers
Cowburn
Sent: zondag 27 december 2009 12:35
To: Flash Coders List
Subject: Re: [Flashcoders] Performance from casting array/vector?
Hi Steve,
I thought it'd be useful to have some actual figures, so I put together a
quick test. Test code was as follows:
package
{
import flash.display.Sprite;
import flash.utils.getTimer;
/**
* @author Piers Cowburn
*/
public class ArrayAccessTest extends Sprite
{
private const ITERATIONS:int = 1000000;
public function ArrayAccessTest()
{
trace("Starting tests...");
var startTimer:int;
var i:int;
var j:int;
var array:Array = [new TestClass(), new TestClass(), new
TestClass()];
var vector:Vector.<TestClass> = new Vector.<TestClass>();
vector.push(new TestClass());
vector.push(new TestClass());
vector.push(new TestClass());
var numItems:int = array.length;
var implicitCast:TestClass;
var explicitCast:TestClass;
var asCast:TestClass;
//
// [Array 0a] Array access test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
array[j].someMethod();
}
}
trace("[Array 0a] Array access test: " +
(getTimer()-startTimer));
//
// [Array 1a] Implicit cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
implicitCast = array[j];
implicitCast.someMethod();
}
}
trace("[Array 1a] Implicit cast test: " +
(getTimer()-startTimer));
//
// [Array 2a] Explicit cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
explicitCast = TestClass(array[j]);
explicitCast.someMethod();
}
}
trace("[Array 2a] Explicit cast test: " +
(getTimer()-startTimer));
//
// [Array 3a] As cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
asCast = array[j] as TestClass;
asCast.someMethod();
}
}
trace("[Array 3a] As cast test: " + (getTimer()-startTimer));
//
// [Array 0b] Array access test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
array[j].someMethod();
array[j].someOtherMethod();
}
}
trace("[Array 0b] Array access test (two operations): " +
(getTimer()-startTimer));
//
// [Array 1b] Implicit cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
implicitCast = array[j];
implicitCast.someMethod();
implicitCast.someOtherMethod();
}
}
trace("[Array 1b] Implicit cast test (two operations): " +
(getTimer()-startTimer));
//
// [Array 2b] Explicit cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
explicitCast = TestClass(array[j]);
explicitCast.someMethod();
explicitCast.someOtherMethod();
}
}
trace("[Array 2b] Explicit cast test (two operations): " +
(getTimer()-startTimer));
//
// [Array 3b] As cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
asCast = array[j] as TestClass;
asCast.someMethod();
asCast.someOtherMethod();
}
}
trace("[Array 3b] As cast test (two operations): " +
(getTimer()-startTimer));
//
// [Vector 0a] Vector access test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
vector[j].someMethod();
}
}
trace("[Vector 0a] Vector access test: " +
(getTimer()-startTimer));
//
// [Vector 1a] Implicit cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
implicitCast = vector[j];
implicitCast.someMethod();
}
}
trace("[Vector 1a] Implicit cast test: " +
(getTimer()-startTimer));
//
// [Vector 2a] Explicit cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
explicitCast = TestClass(vector[j]);
explicitCast.someMethod();
}
}
trace("[Vector 2a] Explicit cast test: " +
(getTimer()-startTimer));
//
// [Vector 3a] As cast test
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
asCast = vector[j] as TestClass;
asCast.someMethod();
}
}
trace("[Vector 3a] As cast test: " + (getTimer()-startTimer));
//
// [Vector 0b] Vector access test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
vector[j].someMethod();
vector[j].someOtherMethod();
}
}
trace("[Vector 0b] Vector access test (two operations): " +
(getTimer()-startTimer));
//
// [Vector 1b] Implicit cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
implicitCast = vector[j];
implicitCast.someMethod();
implicitCast.someOtherMethod();
}
}
trace("[Vector 1b] Implicit cast test (two operations): " +
(getTimer()-startTimer));
//
// [Vector 2b] Explicit cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
explicitCast = TestClass(vector[j]);
explicitCast.someMethod();
explicitCast.someOtherMethod();
}
}
trace("[Vector 2b] Explicit cast test (two operations): " +
(getTimer()-startTimer));
//
// [Vector 3b] As cast test (two operations)
startTimer = getTimer();
for (i = 0; i < ITERATIONS; ++i)
{
for (j = 0; j < numItems; ++j)
{
asCast = vector[j] as TestClass;
asCast.someMethod();
asCast.someOtherMethod();
}
}
trace("[Vector 3b] As cast test (two operations): " +
(getTimer()-startTimer));
}
}
}
And the results were:
[Array 0a] Array access test: 1631
[Array 1a] Implicit cast test: 1527
[Array 2a] Explicit cast test: 1689
[Array 3a] As cast test: 1745
[Array 0b] Array access test (two operations): 3172
[Array 1b] Implicit cast test (two operations): 2636
[Array 2b] Explicit cast test (two operations): 2817
[Array 3b] As cast test (two operations): 2841
[Vector 0a] Vector access test: 1624
[Vector 1a] Implicit cast test: 1520
[Vector 2a] Explicit cast test: 1707
[Vector 3a] As cast test: 1722
[Vector 0b] Vector access test (two operations): 3160
[Vector 1b] Implicit cast test (two operations): 2638
[Vector 2b] Explicit cast test (two operations): 2790
[Vector 3b] As cast test (two operations): 2828
I was surprised to see that using an implicit cast was fastest in all
situations, even faster than using the array access operator to make one
method call (test Array 0a vs. Array 1b). Also, you can see that storing a
temporary variable really starts to make a difference when you're making
more than one method call (difference between Array 0b and Array 1b).
HTH,
Piers
On 27 Dec 2009, at 01:26, Piers Cowburn wrote:
*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