Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is much faster, XMLParser or SimpleXML

What do you think guys? I currently using SimpleXML for my entire project, which have average of 250KB in memory usage w/ 500micro seconds processing per execution. I just plan to switch to XMLParser, your advice is much appreciated.

Edit : The actual microtime is 0.000578 micro seconds. Im just confused in milli and micro, lol.

like image 863
PHPWDev Avatar asked Nov 29 '22 11:11

PHPWDev


1 Answers

In theory at least, XMLParser (a SAX implementation) can be more efficient (speed-wise, but especially memory-wise) than SimpleXML (a DOM implementation), particularly when dealing with larger documents (DOM implementations load the entire XML tree into memory, which can be taxing on resources, whereas SAX implementations allow the programmer to simply traverse the XML without necessarily storing anything in memory, leaving entirely up to you what, if anything, you want to retain in memory) -- see benchmarks comparing various DOM to various SAX parser implementations.

I say can because performing SAX traversal is programatically more complex than traversing or querying the DOM, and a poor use of SAX (or use of SAX in situations where DOM would be better suited anyway, e.g. if at the end of your SAX traversal you ended up loading yourself virtually the entire tree in memory) can actually result in poorer performance than the use of a DOM API.

like image 198
vladr Avatar answered Dec 15 '22 07:12

vladr