|
-
[F9 tip] XML performance
Not sure if anyone is interested, but I just wanted to share this... It's pretty obvious, but hey, it might be useful to some.
I was parsing a rather large XML-file (6398 lines), and it took about 2000ms to complete, which was not really acceptable. The problem was, I was looping through a nodelist, and every iteration I did this :
var tile:XML=xmlMap.layer[3].data.tile[index];
I found out that exactly that line was causing the slowness, because I commented everything that happened before or after it, and it was still slow, however, if I also commented that line, execution was instant. So before the loop, I did this :
var tiles:XMLList=xmlMap.layer[3].data.tile;
And in the loop, I changed it to
var tile:XML=tiles[index];
Now, it seems logical that it would fasten it up a bit, but this tiny modification changed the execution from over 2000ms to 63ms, so that was pretty amazing.
The reason why it's that much faster is that when I did xmlMap.layer[3].data.tile[index], Flash would have to deal with the entire xml-file each time, scanning through the tree to find my data.
By making some more variables like that, I managed to get the execution time down to 31ms.
So, my advice is : always make seperate variables for XML nodes and XMLLists. It makes a world of difference, especially when working with large files. Going from 2000ms to 31ms to parse something is pretty good if you ask me.
-
Senior Member
So, basically you are saying local variables inside the function are faster then global variables? Or is this specific to XML only?
-
It's an XML-thing. When referencing one of the child-nodes with a variable, it won't have to look through the tree each time.
-
Senior Member
-
It is faster because of type casting. You would not have to use a local variable. E.g.
Code:
//-- create sprites
var sprites: Array = new Array();
for ( var i: int = 0; i < 10000; i++ ) sprites.push( new Sprite() );
//-- work witht he sprites SLOW
for ( var i: int = 0; i < 10000; i++ ) { sprites[ i ].x += 2; }
//-- FAST
for ( var i: int = 0; i < 10000; i++ ) { Sprite( sprites[ i ] ).x += 2 };
Another thing is, that array[ 0 ] is slower than array[ int( 0 ) ] because the AVM2 handles all number data by default as Number. But an array-index is just type uint. Since uint is slower than int you may speed things up casting your numbers as int (which is much faster than Number).
-
Custom User Title
 Originally Posted by joa__
Another thing is, that array[ 0 ] is slower than array[ int( 0 ) ]
So, bola[i] is slower them bola[int(i)] ?
I am not sure if i am doing it rigth, but i tested this:
code: var bola:Array = new Array();
var normal:Array = new Array();
var maxNum:int = new int(1000000);
var bolaTime : Number;
var normalTime : Number;
var i : int;
bolaTime = (new Date()).time;
for (i=0; i<maxNum; i++){
bola[int(i)] = "oisa";
}
bolaTime = (new Date()).time - bolaTime;
var n : Number;
normalTime = (new Date()).time;
for (n=0; n<maxNum; n++){
normal[i] = "oisa";
}
normalTime = (new Date()).time - normalTime;
var message : String =
"int(i) version: " + bolaTime + "msn " +
"normal version: " + normalTime + "ms";
trace(message);
And get this:
int(i) version: 406msn normal version: 172ms
Last edited by Incrue; 08-09-2006 at 11:50 AM.
Reason: i want
-
You do not have to cast i as int, since it is int. I made some bost about this on a blog
Now have a look here. Iterating 10000000 times this is the order of fastest method first. First takes between 4.6sec and 4.8sec. The second one could be equivalent. Maybe it was a little subjectiv view here but it is not faster it seems to result more often in results like 4.9sec.
The third is from 4.9sec to 5sec and the last about 5.2sec. But using void() is different from using our own function  )
void( a[int(int(j) % int(360))] );
void( a[int(j % 360)] );
void( a[(j % 360)|0] );
void( a[j % 360] );
The results are
13.5sec for void0( a[int(j % 360)] ) and - surprise, surprise - 13.6sec for void0( a[j % 360] ).
So this reveals for me that it should be done for hardcore optimizations but I think everyday code should use array[i] instead of casting the type correctly because of easy readability. There is no huge speed increase here.
Read more.
a[i] is not slower than a[int(i)] but a[0] is slower than a[int(0)] if i is already typed as int.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|