dongmeiran609914 2013-02-26 12:33
浏览 60
已采纳

DOMXPath的构建有多贵?

When writing a parser of a complex XML document, I wonder if it is OK to construct DOMXPath instances when needed:

function parseData($d) {
    $xpath = new DOMXPath($d);
    // ...
}

function parseMoreData($d) {
    $xpath = new DOMXPath($d);
    // ...
}

$d = new DOMDocument();
$d->loadXML($xml);
parseData($d);
parseMoreData($d);

The alternative would be to create one DOMXPath instance in the beginning, then reuse it everywhere in the parser:

function parseData($d, $xpath) {
    // ...
}

function parseMoreData($d, $xpath) {
    // ...
}

$d = new DOMDocument();
$d->loadXML($xml);
$xpath = new DOMXPath($d);
parseData($d, $xpath);
parseMoreData($d, $xpath);
  • 写回答

2条回答 默认 最新

  • douse8732 2013-06-23 20:22
    关注

    It is very okay to create the DOMXpath instances when you need them, there is not much overhead with these built-in classes in PHP. Especially with DOMDocument / DOMXpath, those merely wrap around features of libxml.

    So what more plays a role how many (different) documents you've opened, not how many DOMXpath objects you have created.

    Also instead of passing a DOMDocument into the parse function(s):

    function parseData(DOMDocument $doc) 
    {
        $xpath = new DOMXPath($doc);
    
        // ...
    }
    

    You can pass the xpath - it carries the document as well:

    function parseData(DOMXpath $xpath) 
    {
        $doc = $xpath->document;
    
        // ...
    }
    

    So your alternative example with two function parameters is technically not necessary at all. As you merely concerned about that detail, this last suggestion should solve your "issue".

    And keep in mind that you normally only need to care about performance when you run into a real problem. Here the code merely improves by injecting the object to operate on instead of creating it inside the function. That's called dependency injection and a much better way to write code: Functions should ask (read: have a parameter) for what they need and not create it their own. They should concentrate on the job (here parsing the data) instead of taking care to instantiate a DOMXPath first.


    The question of O(1) (Big O notation).

    How expensive is new DOMXpath($doc)? Is it O(1)? In comments I answered this with a yes based on what I know from experience and understanding.

    Now I also took a look into lxr. When a new DOMXPath is created (constructor in PHP-C-Code), it's merely a wrapper around a structure in libxml (the underlying C-Library of the DOMDocument extension): xmlXPathContext.

    All this code looks pretty straight forward and only reading/setting single values (not misc size lists or so), so that I'd say now that yes, definitely creating a DOMXPath is O(1).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问