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:

modeTotal timeTime per function callDifference to nocall
nocall57 ms0.006 ms
xslt only98 ms0.010 ms0.004 ms
php native146 ms0.015 ms0.009 ms
php userland194 ms0.019 ms0.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.