From: Peter Lind on
On 27 July 2010 11:27, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
> On Tue, 2010-07-27 at 09:30 +0200, Gary wrote:
>
>> I know there are a number of possible ways of doing this, but I was just
>> wondering if there is an accepted way of doing so which is better
>> than others, performance wise. An idiom, if you like.
>>
>>
>
>
> If you only need to find if a string contains the character and not its
> position, then strstr() (or strchr(), but according to the manual they
> are both the same) is the fastest route to go down. Using strpos() is
> slower because it has to return the actual position of what you were
> looking for, and a regex will be the slowest of all (and one would have
> to question the sanity of someone using a regex to find a single
> character in a string!)
>

I doubt there will be a noticeable difference between strstr and
strpos - returning a bool is not faster than returning an integer.
Would have to check the actual php source to see how the two are
implemented to see if there's any real difference that might make a
difference in speed.

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>
From: Ashley Sheridan on
On Tue, 2010-07-27 at 11:35 +0200, Peter Lind wrote:

> On 27 July 2010 11:27, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
> > On Tue, 2010-07-27 at 09:30 +0200, Gary wrote:
> >
> >> I know there are a number of possible ways of doing this, but I was just
> >> wondering if there is an accepted way of doing so which is better
> >> than others, performance wise. An idiom, if you like.
> >>
> >>
> >
> >
> > If you only need to find if a string contains the character and not its
> > position, then strstr() (or strchr(), but according to the manual they
> > are both the same) is the fastest route to go down. Using strpos() is
> > slower because it has to return the actual position of what you were
> > looking for, and a regex will be the slowest of all (and one would have
> > to question the sanity of someone using a regex to find a single
> > character in a string!)
> >
>
> I doubt there will be a noticeable difference between strstr and
> strpos - returning a bool is not faster than returning an integer.
> Would have to check the actual php source to see how the two are
> implemented to see if there's any real difference that might make a
> difference in speed.
>
> Regards
> Peter
>


I tell a lie! Just checked the manual and strpos() is indeed the faster
than strstr(). I vaguely remembered it was one of the two, but had not
had quite enough coffee at this point of the morning obviously!

Having said that, I agree with Peter. There's unlikely to be a
noticeable difference unless working with thousands of iterations in a
loop using this function call.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Peter Lind on
On 27 July 2010 11:46, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
>
> On Tue, 2010-07-27 at 11:35 +0200, Peter Lind wrote:
>
> On 27 July 2010 11:27, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
> > On Tue, 2010-07-27 at 09:30 +0200, Gary wrote:
> >
> >> I know there are a number of possible ways of doing this, but I was just
> >> wondering if there is an accepted way of doing so which is better
> >> than others, performance wise. An idiom, if you like.
> >>
> >>
> >
> >
> > If you only need to find if a string contains the character and not its
> > position, then strstr() (or strchr(), but according to the manual they
> > are both the same) is the fastest route to go down. Using strpos() is
> > slower because it has to return the actual position of what you were
> > looking for, and a regex will be the slowest of all (and one would have
> > to question the sanity of someone using a regex to find a single
> > character in a string!)
> >
>
> I doubt there will be a noticeable difference between strstr and
> strpos - returning a bool is not faster than returning an integer.
> Would have to check the actual php source to see how the two are
> implemented to see if there's any real difference that might make a
> difference in speed.
>
> Regards
> Peter
>
>
> I tell a lie! Just checked the manual and strpos() is indeed the faster than strstr(). I vaguely remembered it was one of the two, but had not had quite enough coffee at this point of the morning obviously!
>

While on the topic of lack of coffee: I should know better than to
reply without checking the docs. strstr() returns a string, not a bool
- which would make it slower, though again, not noticeably so (unless,
like Ashley points out, you're doing a HUGE number of strstr() calls).

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>