Comparison of the speed and efficiency of the three methods of generating XML files in PHP
- 2020-05-24 05:18:36
- OfStack
Speed comparison of three methods of generating XML files in PHP
There are three ways to do it, one is to write it directly; Use DomDocument; Use SimpleXML;
There's actually a fourth one: XMLWriter, but I haven't used it, and I'm too lazy to try it.
I mainly want to see which of the three ways is faster
Direct access code:
Each is called with a large number of loop operations, and the time is recorded
As expected, the direct writing is the fastest and only takes about 1/3 as long as the other two methods. The other two methods are about the same, and SimpleXML is faster by comparison.
There are three ways to do it, one is to write it directly; Use DomDocument; Use SimpleXML;
There's actually a fourth one: XMLWriter, but I haven't used it, and I'm too lazy to try it.
I mainly want to see which of the three ways is faster
Direct access code:
private function directWriteXml(&$data){
$xmltext='<?xml version="1.0" encoding="UTF-8" ?>';
$xmltext .='<DocumentData>';
$xmltext .='<Detail>';
$loop=count($data);
foreach ($data as $d){
$xmltext .=" <Row ID=\" {$d['id']} \" Name=\" {$d['name']}\" />";
}
$xmltext .='</Detail>';
$xmltext .='</DocumentData>';
return $xmltext;
}
private function useDomDocument(&$data){
// create 1 a XML Document and set XML Version and encoding.
$dom=new DomDocument('1.0', 'utf-8');
// Create the root node
$detail01 = $dom->createElement('Detail');
$dom->appendchild($detail01);
foreach ($data as $d) {
$row = $dom->createElement('Row'," ID=\" {$d['id']} \" Name=\" {$d['name']}\" " );
$detail01->appendchild($row);
}
return $dom->saveXML();
}
private function useSimpleXML(&$data){
// create 1 a XML Document and set XML Version and encoding.
$string = <<<XML
<?xml version='1.0' encoding='utf-8'?>
<detail01>
</detail01>
XML;
$xml = simplexml_load_string($string);
foreach ($data as $d) {
$xml->addChild('Row'," ID=\" {$d['id']} \" Name=\" {$d['name']}\" " );
}
return $xml->asXML(); ;
}
Each is called with a large number of loop operations, and the time is recorded
$loop=10000;
$xml='';
switch($_GET['id']){
case 1:
$ts=$this->microtime_float();
for( $i=0; $i<$loop; $i++)
$xml=$this->directWriteXml($depdata);
$te=$this->microtime_float();
$t=$te-$ts;
$this->assign('times',$t);
$this->assign('method',' Write directly ');
break;
case 2:
$ts=$this->microtime_float();
for( $i=0; $i<$loop; $i++)
$xml=$this->useDomDocument($depdata);
$te=$this->microtime_float();
$t=$te-$ts;
$this->assign('times',$t);
$this->assign('method','DomDocument');
break;
case 3:
$ts=$this->microtime_float();
for( $i=0; $i<$loop; $i++)
$xml=$this->useSimpleXML($depdata);
$te=$this->microtime_float();
$t=$te-$ts;
$this->assign('times',$t);
$this->assign('method','SimpleXML');
break;
}
echo $xml;
As expected, the direct writing is the fastest and only takes about 1/3 as long as the other two methods. The other two methods are about the same, and SimpleXML is faster by comparison.