Re: Why is XML parser so slow?
| From: | terry chay | Date: | Tue, 31 Jul 2001 08:21:07 +0000 |
| Subject: | Re: Why is XML parser so slow? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-60355@lists.php.net to get a copy of this message | ||
No the XML parser is not slow at all. Especially since the only one available in PHP 3 would have to be expat base. Since expat is an event based parser and a very well coded one, you should be able to extract strings from the XML document very easily.
Note that there are some libraries out there which take an XML document and put it into a tree of objects. These might not be fast (I never tried them on PHP < 4.0.4). The correct approach if you need to manipulate the whole XML data store at once is to use a DOM based parser. Since PHP 4.0.2 there has been a DOM based parser marked as experimental. The thing is highly unstable and the PHP functions are very buggy so you'll have to work around those bugs. Also, the functionality is very un-DOMlike and most of the useful method calls aren't implemented.
Oh yeah, the parser leaks memory too. Typically a DOM based parser will use about 1.5x as much memory as the actual XML file, this one seems to use many times more--an double again in size they introduced XPATH support (I think around PHP 4.04pl1 or so). Note that this isn't a true memory leak since all the memory gets returned the httpd and the next PHP processes when the application quits.
However it is quite fast. I built a production web application for my last company with it. Each process (of which 80 or so run simultaneously on each server during peak) uses 100k+ XML file as well as a bunch of smaller (5L) files and the only lag is that the fact that the PHP processes can get so large that the operating system (FreeBSD and Linux) will end up swapping.
Note that a properly coded event based parser (expat) will beat a DOM one almost every time if speed is an issue. Personally, unless you are working on a large data store, the DOM one should be good enough given that everything will have latency once it goes over the internet. In the case of very large data stores, I recommend you install a database that understands XML (Oracle, SQL Server, etc).
Hopefully someone will fix the DOM issues in PHP in the next couple releases.
Take care,
terry