They are large datasets that need visualising & the speed of reading the XML makes it totally un-workable. So I thought I’d put the data into an array & use that as the datasource instead. The speed of the reading the array is waaay faster & the visualisation works really well.
All good then you’d think… er well no… I have to copy the data from the XML into the array on loading. For my largest dataset of about 10,000 elements this takes a couple of minutes… eek! I was expecting a couple of seconds!!
Here’s the very simple code I’m using to populate the array…
[LEFT][COLOR=#993300]
for[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]var[/COLOR] i:[COLOR=#993300]int[/COLOR]=[COLOR=#000000]0[/COLOR]; i<xml.[COLOR=#993300]data[/COLOR].[COLOR=#993300]length[/COLOR]COLOR=#000000[/COLOR]; i++[COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#993300]array[/COLOR][COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] = [COLOR=#993300]xml[/COLOR].[COLOR=#993300]data[/COLOR][COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].@value;
[COLOR=#000000]}[/COLOR]
[/LEFT]
Just a simple loop, 10,000 times - I’m really shocked that pulling the data from the XML is that slow. I know it’s a lot of data, but really, it’s not that much… is it?
Am I missing an obvious trick? Doing something completely dumb? Or am I well & truly scuppered?
After more experimenting it seems I had 2 problems, both not suprising, but the second one’s extent I thought interesting enough to post back.
the for loop contains a check against the xml.length(), this function gets called every iteration & is slow. Simply moving the method call outside the loop & using a variable instead got things moving quicker by a factor of 2. Obvious really - doh!
the var xml is an XMLList as seen in my post, note that the <data> tags are nested inside just 1 <graph> tag. Pulling the data out of this nesting is what appears to be a major performance hog. Removing this (apparantly simple) nesting like this…
var i:int;
var justDataXml:XMLList = xml.data;
var l:int = justDataXml.length();
for(i=0; i<l; i++) {
_data* = justDataXml*.@value;
}
import flash.utils.getTimer;
var n:Number = 1.5;
var test:int;
function divT():void {
var st:Number = getTimer();
for(var i:int=0;i<10000000;i++) {
test = int(n);
}
trace("Time spent calculating with var in the for statement: " +(getTimer()-st)+ " ms");
}
function divB():void {
var st:Number = getTimer();
var i:int = 0;
for(i=0;i<10000000;i++) {
test = int(n);
}
trace("Time spent calculating with var outside for statement: " +(getTimer()-st)+ " ms");
}
divT();
divB();
No I beleive you. Pulling the iterator declaration outside the loop is just a coincedence - in my project I have another 3 loops after this & I wanted to use the same iterator.
The main point was about the huge difference in performance i found in getting data from elements compared to nested elements in an XMLList.
Declaring the length as a fixed variable is a good one, but I don’t always remember to declare the exact node to loop through outside the loop. Good call.