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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条