On 19 Mrz., 17:18, Toby A Inkster <usenet200...@t obyinkster.co.u k>
wrote:
Marian Steinbach wrote:
3) Writing XML "manually"
like $xml .= '<tag>'.utf8_en code($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>'.htmls pecialchars($sh ort).'</short>'."\n";
}
if (isset($long) && !empty($long)) {
$ret .= '<long>'.htmlsp ecialchars($lon g).'</long>'."\n";
}
$ret .= '</document>';
return $ret;
}
for ($i=0; $i<$num_iterati ons; $i++) {
$xml = makeXmlDocument ($short_string, $long_string);
}
echo 'time used: '.(microtime(tr ue) - $start);
?>
xmlwriter.php:
<?php
$start = microtime(true) ;
require './common.inc.php' ;
function makeXmlDocument ($short, $long) {
$memory = xmlwriter_open_ memory();
xmlwriter_start _document($memo ry,'1.0','UTF-8');
xmlwriter_start _element($memor y, 'document');
if (isset($short) && !empty($short)) {
xmlwriter_write _element($memor y, 'short', $short);
}
if (isset($long) && !empty($long)) {
xmlwriter_write _element($memor y, 'long', $long);
}
xmlwriter_end_e lement($memory) ;
$ret = xmlwriter_outpu t_memory($memor y, true);
return $ret;
}
for ($i=0; $i<$num_iterati ons; $i++) {
$xml = makeXmlDocument ($short_string, $long_string);
}
echo 'time used: '.(microtime(tr ue) - $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