You are here: Re: Change alternate commas to semi colons... « PHP Programming Language « IT news, forums, messages
Re: Change alternate commas to semi colons...

Posted by Rik on 06/21/06 11:32

Bent Stigsen wrote:
> 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.

Yup, I should've tested it, somtimes lazyness will make you look like a fool
:-)

Testing here, 5000 times, string 4 times as long:
Regex does 1.0872719287872

> 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;
> }
> }

Does it in 6.8535070419312

> 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.

strpos() example:
$a = 0;
$bool = false;
while(strpos($authors,',',$a)!== false){
$a = strpos($authors,',',$a);
if($bool) $authors[$a] = ';';
$a++;
$bool = !$bool;
}

This manages it in 2.1639029979706

Regex still seems to win this.
I thought about using array_reduce(explode(',',$authors),
'some_custom_function'), but that also doesn't even get close.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация