# Should reading XML be this slow?

**URL:** <https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615>\
**Category:** Uncategorized\
**Created:** [June 27, 2008, 7:22pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615 "2008-06-27T19:22:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bombdogs](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Bombdogs](https://forum.kirupa.com/u/Bombdogs)\
**Post date:** [June 27, 2008, 7:22pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/1 "2008-06-27T19:22:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bombdogs](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Bombdogs](https://forum.kirupa.com/u/Bombdogs)\
**Post date:** [June 27, 2008, 11:00pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/2 "2008-06-27T23:00:10Z")

</div>

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…

```php
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

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 27, 2008, 11:10pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/3 "2008-06-27T23:10:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 27, 2008, 11:41pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/4 "2008-06-27T23:41:05Z")

</div>

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

```auto

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

```

Is exactly as fast or slow as

```auto

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

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 27, 2008, 11:43pm UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/5 "2008-06-27T23:43:15Z")

</div>

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

```auto

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();

```

---

<div class="post-metadata">

**Author:** ![Bombdogs](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Bombdogs](https://forum.kirupa.com/u/Bombdogs)\
**Post date:** [June 28, 2008, 12:25am UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/6 "2008-06-28T00:25:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 28, 2008, 1:56am UTC](https://forum.kirupa.com/t/should-reading-xml-be-this-slow/264615/7 "2008-06-28T01:56:47Z")

</div>

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.
