Which is much faster, XMLParser or SimpleXML

偶尔善良 提交于 2020-01-02 06:08:52

问题


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.


回答1:


In most cases, XML Parser will be much slower both in terms of development and execution, mainly because you have to write/execute tons of userland PHP code to navigate/read your document.

In some cases, you can find some improvement using XMLReader (not sure about XML Parser) but the cost is extra development time and harder maintenance. Since your application already uses SimpleXML and only uses 250 KB, your time will most likely be better spent on other areas of your application. 500ms seems quite a lot for some XML processing with SimpleXML, you should profile that and/or post your most CPU-intensive routine in another question for optimization.




回答2:


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.




回答3:


Yes, XML Parser (not to confuse with XMLParser ) is preferred if you have memory concerns, because it doesn't require the whole file to be pre-loaded and pre-parsed before use. It's like fgets() or fread() compared to file_get_contents()



来源:https://stackoverflow.com/questions/2604277/which-is-much-faster-xmlparser-or-simplexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!