| 
	
 | 
 Posted by Marian Steinbach on 03/19/07 21:47 
On 19 Mrz., 17:18, Toby A Inkster <usenet200...@tobyinkster.co.uk> 
wrote: 
> Marian Steinbach wrote: 
> > 3) Writing XML "manually" 
> > like $xml .= '<tag>'.utf8_encode($value).'</tag>'; 
> 
> I tend to do this. Don't forget liberal use of htmlentities(). Unlike 
> parsing XML, outputting XML manually is very easy, and I find that most XML 
> output libraries fall into the "overkill" category. 
 
 
I just did a very simple comparison of "manual XML writing" and 
XMLWriter (which is supposed to be very fast). Surprisingly to me, 
XMLWriter is even twice as fast as manual XML writing. 
 
I ran creation of a simple document 10.000 times. Results: 
 
manual.php: ~ 5 seconds 
xmlwriter.php: ~ 2 secs 
 
Here is the code I used: 
 
manual.php: 
 
<?php 
 
$start = microtime(true); 
 
require './common.inc.php'; 
 
function makeXmlDocument($short, $long) { 
    $ret = '<?xml version="1.0" encoding="UTF-8" ?> 
    <document>'."\n"; 
    if (isset($short) && !empty($short)) { 
        $ret .= '<short>'.htmlspecialchars($short).'</short>'."\n"; 
    } 
    if (isset($long) && !empty($long)) { 
        $ret .= '<long>'.htmlspecialchars($long).'</long>'."\n"; 
    } 
    $ret .= '</document>'; 
    return $ret; 
} 
 
for ($i=0; $i<$num_iterations; $i++) { 
    $xml = makeXmlDocument($short_string, $long_string); 
} 
 
echo 'time used: '.(microtime(true) - $start); 
?> 
 
xmlwriter.php: 
 
<?php 
 
$start = microtime(true); 
 
require './common.inc.php'; 
 
function makeXmlDocument($short, $long) { 
    $memory = xmlwriter_open_memory(); 
    xmlwriter_start_document($memory,'1.0','UTF-8'); 
    xmlwriter_start_element($memory, 'document'); 
    if (isset($short) && !empty($short)) { 
        xmlwriter_write_element($memory, 'short', $short); 
    } 
    if (isset($long) && !empty($long)) { 
        xmlwriter_write_element($memory, 'long', $long); 
    } 
    xmlwriter_end_element($memory); 
    $ret = xmlwriter_output_memory($memory, true); 
    return $ret; 
} 
 
for ($i=0; $i<$num_iterations; $i++) { 
	$xml = makeXmlDocument($short_string, $long_string); 
} 
 
echo 'time used: '.(microtime(true) - $start); 
?> 
 
common.php: 
 
<?php 
 
$num_iterations = 10000; 
 
$short_string = 'Some Short Value'; 
 
$long_string = 'Some very long text (truncated here)'; 
 
?> 
 
It would be interesting to see which version scales better... That 
means: How would the two behave with multiple concurrent requests 
(rather typical for a REST API, I guess) and with very large XML 
documents (rather untypical for REST services, since lists should be 
limited anyway)? 
 
I'll test that. 
 
Marian
 
  
Navigation:
[Reply to this message] 
 |