Performance of is operator in Flash / ActionScript 3?
Anyone have any articles / tests on the performance of the is operator? I can't find anything on Google, it's just eating my "is" keyword as being too small.
I plan to use the is operator extensively in a messaging system for my components and so performance needs to be solid. It'll save me from having to come up with a scheme of ids and id-lookups for each message if I can just do:
function onMessage(message : Message, type : Class) : void
{
switch(type)
{
case MessageOne:
// whatever
break;
case MessageTwo:
// whatever
break;
}
}
The timing tests I've done show it's almost as fast as an integer comparison, so I just want to ma开发者_StackOverflowke sure.
Anyone done their own tests or know some articles?
Thanks.
The "is" operator is plenty fast, even at tens of thousands of tests per second.
Not only that, it really is a best practice for comparing inheritance hierarchy, not just class name (so comparing if Image is UIComponent
for example) as well as support for implementation of interfaces (so comparing Image is IEventDispatcher
for example).
More: http://livedocs.adobe.com/flex/3/html/03_Language_and_Syntax_09.html#122921
So, yes, it's fast enough - and, if it wasn't and you're having to break the foundational best practices of the language to bend it to the will of your design - then you're doing it wrong.
:)
This is
operator is a bit slower.
My tests looked something like this :
var obj:MyObj = new MyObj();
// Index type comparison
for(var a:int; a<1000000; a++){
if(obj.type == OBJ_TYPE1) continue;
if(obj.type == OBJ_TYPE2) continue;
...
}
// 'is' type comparison
var obj:MyObj = new MyObj();
for(var a:int; a<1000000; a++){
if(obj is ObjectType1) continue;
if(obj is ObjectType1) continue;
...
}
I performed a million iterations and during each iteration I performed 90 tests (a total of 9 000 000 tests). The results were as follows (an average over ten tests) :
is operator : 1974 ms
int static const compare : 210.7 ms
int instance const compare : 97 ms
int literal compare : 91.9 ms
A note about the last three test results just in case it isn't clear enough : each one still does an integer comparison but the tests differ in how the types are stored :
// For the second test the types are stored as static constants
// int static const compare : 97.4 ms
private static const OBJ_TYPE1:int = 0;
private static const OBJ_TYPE2:int = 1;
...
// For the third test the types are stored as instance constants
// int member const compare : 1319.9 ms
private const OBJ_TYPE1:int = 0;
private const OBJ_TYPE2:int = 1;
...
// Here the types are not stored anywhere but instead literals
// are used for each test
// int literal compare : 91.9 ms
for(var a:int; a<1000000; a++){
if(obj.type == 0) continue; // OBJ_TYPE1
if(obj.type == 1) continue; // OBJ_TYPE2
...
}
So basically the integer comparisons are always faster and if you use literal values or instance variables the difference is huge (You can even use configuration constants here to keep your code neat without the performance hit of using variables ) .
精彩评论