我创建了性能基准测试:XMLReader vs DOMDocument
XMLReader.php:
$script_starttime = microtime(true);
libxml_use_internal_errors(true);
$xml = new XMLReader;
$xml->open($_FILES["file"]["tmp_name"]);
$xml->setSchema($xmlschema);
while (@$xml->read()) {};
if (count(libxml_get_errors())==0) {
echo 'good';
} else {
echo 'error';
}
echo '<br><br>Time: ',(microtime(true) - $script_starttime)*1000," ms, Memory: ".memory_get_usage()." bytes";
DOMDocument.php:
$script_starttime = microtime(true);
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xmlfile = $_FILES["file"]["tmp_name"];
$xml->load($xmlfile);
if ($xml->schemaValidate($xmlschema)) {
echo 'good';
} else {
echo 'error';
}
echo '<br><br>Time: ',(microtime(true) - $script_starttime)*1000," ms, Memory: ".memory_get_usage()." bytes";
我的实施例:18 MB XML与258.230线
结果:
的XMLReader - 656.14199638367毫秒,379064个字节
DOM文档 - 483.04295539856毫秒,369280个字节
所以我决定去与DOM文档,只是用自己的XML和XSD尝试并使用你的更快的选择。
我无法使用DOM,因为我的XML文件很大,DOM无法处理。 – BentCoder 2012-07-30 11:50:19
@DaveRandom DOM功能强大,但该功能需要花费。这两种工具都有完全不同的用例,并且以各自的方式强大。与XMLReader相比,dom非常昂贵。 DOM会将整个文档加载到内存中。 XMLReader使您能够更高效地读取文档,并且可以在需要时将单个节点加载到dom中。您将能够获得更好的性能并节省(可能性很大)的资源。对于处理较小XML文件的应用程序也是如此。 “正确的”选择取决于要求。两者都不是“更好”。 – 2015-05-28 00:30:21