Hi list, I'm using the package XML to create a simple XML document. Unfortunately constructing the XML tree is extremely slow. My code (see below) adds only about 100 nodes per second on an Intel i5 machine. There's clearly something wrong but I don't see what. Here's a sample of the XML document: <?xml version="1.0" encoding="utf-8"?> <MarkerSet xmlns="http://www.brainproducts.com/MarkerSet"> <SamplingRate>512.032770097286</SamplingRate> <SamplingInterval>1.953</SamplingInterval> <Markers> <Marker> <Type>Stimulus</Type> <Description>prr_156</Description> <Position>397497</Position> <Points>1</Points> <Channel>All</Channel> </Marker> ... </Markers> </MarkerSet> There are about 500 very similar marker tags. Here's the part of the code that is particularly slow (x is a character, y is an integer, both have a length of about 500): markernode <- xmlNode('Markers') for(i in 1:length(t)){ markernode <- addChildren(markernode, xmlNode('Marker', xmlNode('Type', 'Stimulus'), xmlNode('Description', x[i]), xmlNode('Position', y[i]), xmlNode('Points', 1), xmlNode('Channel', 'All'))) } Looks pretty harmless to me. Any suggestion about how I can speed this up are most welcome! Titus
>>>>> On Fri, 10 Feb 2012 13:18:38 +0100, >>>>> Titus von der Malsburg (TvdM) wrote:> Hi list, > I'm using the package XML to create a simple XML document. > Unfortunately constructing the XML tree is extremely slow. My code > (see below) adds only about 100 nodes per second on an Intel i5 > machine. There's clearly something wrong but I don't see what. Have you considered simply writing the XML using standard functions like cat() and friends? That can be much faster than creating an XML object and writing that out (have just done that to write exams into our e-learning platform via XML). Best, Fritz
On Fri, Feb 10, 2012 at 1:40 PM, Friedrich Leisch wrote:> Have you considered simply writing the XML using standard functions > like cat() and friends? That can be much faster than creating an XML > object and writing that out (have just done that to write exams into > our e-learning platform via XML).Friedrich, thanks for your response. Since my current XML document is fairly simple, I could of course just paste some pre-fabricated strings together. For more complex documents this would however not be a satisfying solution. The reason I though I should ask on the list is that it seems to me that the extremely poor performance could be due to a bug in the XML package. Titus
Le vendredi 10 f?vrier 2012 ? 13:18 +0100, Titus von der Malsburg a ?crit :> Hi list, > > I'm using the package XML to create a simple XML document. > Unfortunately constructing the XML tree is extremely slow. My code > (see below) adds only about 100 nodes per second on an Intel i5 > machine. There's clearly something wrong but I don't see what. > > Here's a sample of the XML document: > > <?xml version="1.0" encoding="utf-8"?> > <MarkerSet xmlns="http://www.brainproducts.com/MarkerSet"> > <SamplingRate>512.032770097286</SamplingRate> > <SamplingInterval>1.953</SamplingInterval> > <Markers> > <Marker> > <Type>Stimulus</Type> > <Description>prr_156</Description> > <Position>397497</Position> > <Points>1</Points> > <Channel>All</Channel> > </Marker> > ... > </Markers> > </MarkerSet> > > There are about 500 very similar marker tags. Here's the part of the > code that is particularly slow (x is a character, y is an integer, > both have a length of about 500): > > markernode <- xmlNode('Markers') > for(i in 1:length(t)){ > markernode <- addChildren(markernode, xmlNode('Marker', > xmlNode('Type', 'Stimulus'), xmlNode('Description', x[i]), > xmlNode('Position', y[i]), xmlNode('Points', 1), > xmlNode('Channel', 'All'))) > } > > Looks pretty harmless to me. Any suggestion about how I can speed > this up are most welcome!Just a guess, but I'd try creating all 'Marker' nodes first, storing them in a 'markers' list, and then calling addChildren(markernode, kids=markers). Adding nodes one by one is likely to create useless overhead. Hope this helps