Re: Regex in PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jun 4, 2008 at 11:43 PM, Robert Cummings <robert@xxxxxxxxxxxxx>
wrote:

> On Wed, 2008-06-04 at 23:20 -0400, Nathan Nobbe wrote:
> >
> > i repeated your test using the time program and splitting the script into
> 2,
> > one for each strpos and stripos, to find similar results.  imo, there is
> no
> > need for 2 comparisons for case-insensitive searches, because both
> arguments
> > can be converted to a single case prior to the search.  obviously, there
> is
> > a small amount of overhead there the case-sensitive search is
> unencumbered
> > by.  i guess i never sat down and thought about how that algorithm would
> > work (case-sensitive) =/.
> >
> > thanks for the tips rob.  sorry to bother you richard.
>
> You would do two comparisons... why incur the overhead of a conversion
> if one is not necessary.


because it simplifies the algorithm, there is no need for conditional logic.


> First you do case sensitive match, if that
> fails then you try the alternative version comparison. It is inefficient
> to perform 2 conversions and a single comparison in contrast.


3 operations vs. 1 or potentially 2, sure.


> Similarly,
> it's very inefficient to convert two entire strings then perform a
> comparison.


then they could be converted one at a time as the strings were traversed to
increase efficiency.


> If the first characters differ then conversion of the rest
> of the strings was pointless.


good point.


> This is basic algorithms in computer
> science.
>

you really know how to rub it in there rob.  but i was looking at the
implementation in the php code, looks like somebody likes my idea (this code
found in ext/standard/string.c).  on the second line the haystack is
converted to lower case[1], then if it passes a couple of checks, the needle
is converted to lower case[2], and lastly the comparison is performed[3].
there is no logic to check both cases.
(i have placed a star beside the statements ive referred to).
...
    haystack_dup = estrndup(haystack, haystack_len);
*[1]    php_strtolower(haystack_dup, haystack_len);

    if (Z_TYPE_P(needle) == IS_STRING) {
        if (Z_STRLEN_P(needle) == 0 || Z_STRLEN_P(needle) > haystack_len) {
            efree(haystack_dup);
            RETURN_FALSE;
        }

        needle_dup = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
*[2]        php_strtolower(needle_dup, Z_STRLEN_P(needle));
*[3]        found = php_memnstr(haystack_dup + offset, needle_dup,
Z_STRLEN_P(needle), haystack_dup + haystack_len);
    }
...

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux