Home  >  Article  >  Backend Development  >  Comparison of speed and efficiency of three methods of generating XML files in PHP_PHP Tutorial

Comparison of speed and efficiency of three methods of generating XML files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:44804browse

Comparison of the speed of three methods of generating XML files in PHP
There are three methods, namely writing directly; using DomDocument; using SimpleXML;
In fact, there is a fourth method: using XMLWriter, but I have never used it, so I’m too lazy to try it.
Mainly I want to see which of the three methods is faster
Directly enter the code:

Copy the code The code is as follows:

private function directWriteXml(&$data){
$xmltext='';
$xmltext .='';
$xmltext .='';
$loop=count($data);
foreach ($data as $d ){
$xmltext .=" ";
}
$xmltext .='
';
$xmltext .='
';
return $xmltext;
}
private function useDomDocument(&$data) {
// Create an XML document and set the XML version and encoding. .
$dom=new DomDocument('1.0', 'utf-8');
// Create 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 an XML document and set the XML version and encoding. .
$string = <<


XML;
$xml = simplexml_load_string($string);
foreach ($data as $d) {
$xml->addChild('Row'," ID=" {$d ['id']} " Name=" {$d['name']}" " );
}
return $xml->asXML(); ;
}

Add a large number of loop operations to each call and record the time
Copy the code The code is as follows:

$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;

The actual test results are as expected, direct writing is the fastest, and the time consumption is only about 1/3 of other methods. The other two methods are similar, and SimpleXML is faster in comparison.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326072.htmlTechArticleComparison of the speed of the three methods of generating XML files in PHP There are three methods, namely writing directly; using DomDocument; Use SimpleXML; Actually there is a fourth option: use XMLWriter, but I have never used it and am too lazy to try it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn