|
Posted by Andy Jeffries on 06/21/06 08:03
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:
> : Regular expressions can be used, I'd think this is faster:
>
> I wouldn't assume that a block of php code is faster than simply calling
> preg_replace. In fact I wouldn't be surprised if it's the other way round
> - preg_replace may be faster.
>
> I would be interested to see the result of a proper comparison between the
> two.
Ask and ye shall receive:
I've done a small script that runs each version 5000 times (never guess
which is faster, always test). And the results are:
Function call: 0.0749678611755 seconds
PREG Replace: 0.0623338222504 seconds
So the preg version is faster (it completes in 83% of the time).
Now, there's another upside. In the way that Rik coded them, there's also
a subtle difference in the two outputs. The Function call version puts an
extra ; on the end (which would result in an empty element if exploded and
would therefore need to be tested for before creating a DB row or the like).
Also, that test version used the original authors string. If I make this
string 10 times as long, the times are as follows:
Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds
So, the preg version absolutely whips the function call (completes in 24%
of the time).
As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.
Hope this is interesting to someone.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos
Navigation:
[Reply to this message]
|