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 Bent Stigsen on 06/21/06 13:00

Rik wrote:

> 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
> :-)

Acceptable tradeoff :)


> 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

Bugger. I'm a bit surprised it's slower, but that much. Useless piece of
[bleep].

Did you run it more than once. I mean, it couldn't have been some other app.
hogging the cpu, at that moment.


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

You do strpos twice, and it can bee squeezed a bit.

Try this instead:

$a = -1;
$bool = true;
while (($a=strpos($authors, ',', $a+1))!==false)
if ($bool=!$bool) $authors[$a] = ';';


> Regex still seems to win this.

There could allways be made a AlternateCommaReplacer module.

[snip]

--
/Bent

 

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

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