Should reading XML be this slow?

Hi all,

I have some XML like this…

[COLOR=#000080]<graph>[/COLOR]
[COLOR=#000080]<data date=[COLOR=#0000ff]“1-1-08”[/COLOR] value=[COLOR=#0000ff]“25”[/COLOR] />[/COLOR]
[COLOR=#000080]<data date=[COLOR=#0000ff]“2-1-08”[/COLOR] value=[COLOR=#0000ff]“40”[/COLOR] />[/COLOR]
[COLOR=#000080]</graph>[/COLOR]

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?

Thanks in advance,

PMF

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.

  1. 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!

  2. 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;
}

saw the performance improve by a factor of 1000!!

Suprising, but all good learning I suppose.

PMF

was gonna post the length thing but I’m really surprised to hear doing

var i:int
for(i= …)

is a big difference performance wise from

for(var i:int …)

Nice to know, however.

actually I just timed that, and it has no impact on the loop performance.


var i:int;
for(i=0...

Is exactly as fast or slow as


for (var i:int=0 ...)

edit: if you don’t believe me, try this :wink:


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.

PMF

Good job figuring out. :thumb:

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.