...composed of an indefinite, perhaps infinite number of hexagonal galleries...

© 1994-2017. David Sklar. All rights reserved.

Speed: DOM traversal vs. XPath in PHP 5

Needing to pick out some bits from a smallish (40 lines) XML document studded with namespaces, I first turned to DomXPath:

$doc = new DomDocument();
$x = new DomXPath($doc);
$nodes = $x->query('/one:tag/two:*');

Worked great, code’s concise, XPath expression is simple. But I had the nagging thought that using DOM traversal functions should be faster. So I tried:

$doc = new DomDocument();
$firstNode =
$nodes =

The DOM traversal code is about four times faster than the XPath code. I’m going to use the XPath code, though.

That 4x speed multiple translates into about a half second to execute for the XPath code and about 0.13 seconds to execute for the DOM code when each is run 10,000 times. Since a typical use of this code will involve it running maybe 10 or 20 times during a request, I’m happy to sacrifice a few microseconds of processor time in exchange for simpler code. (Since, to me, the XPath expression is simpler and has the handy property of scooping up all the nodes that match – the DOM code would have to be modified to loop through multiple nodes in $firstNode to get that behavior. If XPath gives you the willies and you prefer using the DOM functions, then you’re in great shape.)

Whenever performance evaluation issues like this come up, I’m reminded of Adam’s mini-rant that I wrote about in 2004.

Tagged with php