MySQL+PHP vs XSLT mini rumble
February 29th, 2008I use XML+XSLT instead of MySQL+PHP on 2 of my websites. For some reason I always imagined XSLT is fast. Yesterday I added something made with X+X combination to QUBIDRAW also so I decided to do a small comparison because my “is fast” assumption was based on.. well, nothing.
I made two scripts called tablepush that pushes some tabular data and shape it into html table. One script uses basic mysql functions and then php to do the html shaping:
<?php
$link = mysql_connect('localhost:3307', 'root', 'rootp');
mysql_select_db('mlvsx');
$query = 'SELECT * FROM '.$_GET['data'];
$result = mysql_query($query);
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
foreach ($line as $k => $col_value) {
$tag = '';
if ($k == 1) $tag = 'strong';
else if ($k == 2) $tag = 'em';
$t1 = ''; $t2 = '';
if ($tag) {
$t1 = "<$tag>";
$t2 = "</$tag>";
}
echo "<td>{$t1}{$col_value}{$t2}</td>";
}
echo "</tr>";
}
echo "</table>";
mysql_free_result($result);
mysql_close($link);
?>
The other loads XML and uses XSLT to turn it to html table:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="no"/>
<xsl:template match="things">
<table>
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="thing">
<tr>
<td><xsl:value-of select="@id" /></td>
<xsl:apply-templates />
</tr>
</xsl:template>
<xsl:template match="aaa"><td><strong><xsl:value-of select="." /></strong></td></xsl:template>
<xsl:template match="bbb"><td><em><xsl:value-of select="." /></em></td></xsl:template>
<xsl:template match="ccc"><td><xsl:value-of select="." /></td></xsl:template>
</xsl:stylesheet>
Results using http_load to do the load test on my local (older) computer are :
Table with 3 rows
my-php : 25 requests / sec
X-X : 49 requests / sec
Table with 50 rows
my-php : 21 requests / sec
X-X : 39 requests / sec
Table with 350 rows
my-php : 11 requests / sec
X-X : 13 requests / sec
So it seems XSLT is quite fast. I will keep using it for the cases where it makes sense (like a lot of discrete data with known and non-frequent relations). It's funny to me that XSLT is in a way quirky and hackish and yet elegant. you can find complete sources and data to do the test here: LAMP XSLT .
It would be interesting to test some other platforms too...

January 31st, 2010 at 7:37 pm
If you’re not already doing so, caching the parsed XSL object will probably make it faster.