mhl*_*as7 7 php rss performance simplexml domdocument
我正在使用SimpleXML类构建RSS解析器,我想知道使用DOMDocument类是否会提高解析器的速度.我正在解析一个至少1000行的rss文档,我使用了来自这1000行的几乎所有数据.我正在寻找花费最少时间来完成的方法.
Fra*_*ila 20
SimpleXML并且DOMDocument都使用相同的parser(libxml2),因此它们之间的解析差异可以忽略不计.
这很容易验证:
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:\n";
foreach ($methods as $method) {
echo $method,": ",$method($xml,$reps), "\n";
}
}
main();
Run Code Online (Sandbox Code Playgroud)
在我的机器上我基本上没有区别:
Time to complete 10000 reps:
time_load_dd: 17.725028991699
time_load_sxe: 17.416455984116
Run Code Online (Sandbox Code Playgroud)
这里真正的问题是您使用的算法以及您使用的数据.1000行不是一个大的XML文档.您的减速不会在内存使用或解析速度上,而是在您的应用程序逻辑中.
| 归档时间: |
|
| 查看次数: |
11763 次 |
| 最近记录: |