dsdvr06648 2012-02-20 19:18
浏览 84
已采纳

SimpleXML与DOMDocument性能

I am building an RSS parser using the SimpleXML Class and I was wondering if using the DOMDocument class would improve the speed of the parser. I am parsing an rss document that is at least 1000 lines and I use almost all of the data from those 1000 lines. I am looking for the method that will take the least time to complete.

  • 写回答

2条回答 默认 最新

  • dony113407 2012-02-20 23:33
    关注

    SimpleXML and DOMDocument both use the same parser (libxml2), so the parsing difference between them is negligible.

    This is easy to verify:

    function time_load_dd($xml, $reps) {
        // discard first run to prime caches
        for ($i=0; $i < 5; ++$i) { 
            $dom = new DOMDocument();
            $dom->loadXML($xml);
        }
        $start = microtime(true);
        for ($i=0; $i < $reps; ++$i) { 
            $dom = new DOMDocument();
            $dom->loadXML($xml);
        }
        $stop = microtime(true) - $start;
        return $stop;
    }
    function time_load_sxe($xml, $reps) {
        for ($i=0; $i < 5; ++$i) { 
            $sxe = simplexml_load_string($xml);
        }
        $start = microtime(true);
        for ($i=0; $i < $reps; ++$i) { 
            $sxe = simplexml_load_string($xml);
        }
        $stop = microtime(true) - $start;
        return $stop;
    }
    
    
    function main() {
        // This is a 1800-line atom feed of some complexity.
        $url = 'http://feeds.feedburner.com/reason/AllArticles';
        $xml = file_get_contents($url);
        $reps = 10000;
        $methods = array('time_load_dd','time_load_sxe');
        echo "Time to complete $reps reps:
    ";
        foreach ($methods as $method) {
            echo $method,": ",$method($xml,$reps), "
    ";
        }
    }
    main();
    

    On my machine I get basically no difference:

    Time to complete 10000 reps:
    time_load_dd: 17.725028991699
    time_load_sxe: 17.416455984116
    

    The real issue here is what algorithms you are using and what you are doing with the data. 1000 lines is not a big XML document. Your slowdown will not be in memory usage or parsing speed but in your application logic.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀