After Rob's idea from yesterday about using XMLReader within XSLT I was wondering, how much of a slowdown calling PHP functions from XSLT is.
I wrote 4 different XSLT templates, which do a simple substring. One with the xslt function âsubstringâ, one with just calling the native PHP function âsubstrâ and one with calling a user-defined function (which is also just calling âsubstrâ). I called this 100 times (with one of those great recursive self-calling xslt-templates) and did call the âtransformToXMLâ function a 100 times for each stylesheet. This means, we called the function 10'000 times for each benchmark run.
For comparison, I also did an XSLT with no function call at all, just the recursive template and a simple xsl:value-of.
The results:
mode | Total time | Time per function call | Difference to nocall |
nocall | 57 ms | 0.006 ms | â |
xslt only | 98 ms | 0.010 ms | 0.004 ms |
php native | 146 ms | 0.015 ms | 0.009 ms |
php userland | 194 ms | 0.019 ms | 0.014 ms |
Calling PHP functions from XSLT is significantly slower, if you look at these figures, but IMHO opinion the difference is somehow negligible. You're usually not calling thousands of PHP functions per run, and even if you do, your XSLT template may be a little bit more complex than the example and the difference to the whole should not be really recognizable. And you gain a lot of functionality, which is just not possible with XSLT only otherwise or would need some really awkward workarounds (which almost certainly will slow down the transformation also :) )
And here's the PHP script itself.