-
- 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 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(); ;
- }
- ?>
Copy code
When calling, add a large number of loop operations to each one, and record the time .
-
- $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;
- ?>
-
Copy code
Actual test results: Direct writing is the fastest, and the time consumption is only about 1/3 of other methods. The other two methods are almost the same, and SimpleXML is faster in comparison.
|