|
Posted by Bent Stigsen on 06/21/06 10:33
Andy Jeffries wrote:
> 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.
[snip]
> 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.
Indeed preg_replace has the advantage of doing its looping in compiled code,
but throwing around with arrays is not cheap, so I think the methods are
too different to make a fair comparison.
I would think that in this case, since the logic is quite simple, doing a
straightforward character by character sweep, just like preg_replace would
do for its pattern matching, ought to be faster, unless PHP really really
*really* stinks at doing loops in phpcode of course.
Can you run the same test with this code:
$alternate_comma = false;
$length = strlen($authors);
for ($i=0; $i<$length; $i++) {
if ($authors[$i]==',') {
if ($alternate_comma) $authors[$i] = ';';
$alternate_comma = !$alternate_comma;
}
}
Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would depend on
how well php does it.
--
/Bent
Navigation:
[Reply to this message]
|