Hi Josh,

>I'm an experienced PHP programmer and wanted some opinions from
>peers -- I am sending results from a MySQL database as XML data
>using PHP.
Why? (more about that question below...)

>I can either build the entire XML document in memory using
>$xml .= "<node>{$data}</node>"; repeatedly or I can do
>echo "<node>{$data}</node>";. The total side of the data may be
>about 512KB or maybe even 1MB.
>
>Opinions on whether echo or .= will provide better performance?
I would drectly echo the results. Think it is both faster and requires less
resources from the server.
Faster because generally what limits the speed is the data transfer rate,
not the rate at which data are output from buffer. That means that when
echoing, the first data are already on their way when the last data are
still being assembled. Building the whole output in memory and only then
sending it will require the same amount of time to send (receive) the data
but sending will have started later.

Then at this moment the size of the data is 512 KB, maybe 1 MB, but what
about the future? There may be a moment that the amount of data becomes too
big to assemble the whole thing in memory.

Then back to the XML thing...

I reently had to build an XML parser because somebody was making data for a
client of mine available, but in XML format only. A few thousand rows of
three fields: two numeric and one a keyword. It would have been much more
efficient if the data were transmitted in csv format:

For each line, containing data like "1,2,keyword" about 20 bytes would have
been used and looping through

$Values = fgetsv($File, 20, ",");
$Query = "INSERT INTO MyTable (PK, FK, keyword) VALUES (".$Values[0].",
".$Values[1].", '".$Values[2]."');";

would have been sufficient.

The XML file I was to receive was about 8 times the size of the size the csv
file (unnecessarily slow bacause needing to be downloaded from another
server), as the same data were formatted like this:
<row>
        <primary_key>1</primary key>
        <foreign_key>2</foreign_key>
        <keyword>keyword</keyword>
</row>

Apart from that, it took quite a bit more of code to parse the XML, store
the values read from each five lines of XML into one array of variables and
then construct the query from that.

XML may be nice when sending or receiving lots of formatted text, when the
choice of a suitable separator for the csv may be complicated (and each
datafield containing lots of data the relative overhead of the XML tags
becomes less) but when sending large amounts of simple data I would consider
another format...

Marc

Reply via email to