Commits ------- fe62401 optimized string starts with checks Discussion ---------- optimized string starts with checks Doing this with strpos() is slightly faster than substr(). ``` Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - ``` --------------------------------------------------------------------------- by vicb at 2012-01-11T19:58:27Z How faster ? even if the string is long and do not contain an occurrence of the sub-string ? Looks like micro-(not)-optimizations to me. --------------------------------------------------------------------------- by kriswallsmith at 2012-01-11T20:04:26Z The difference is about 0.1s when repeated 1M times. --------------------------------------------------------------------------- by vicb at 2012-01-11T20:08:12Z % would be better (machine & env independant), what string size, what match offset ? I personally vote against (`substr` is more meaningful to me and I do not like micro-optims) --------------------------------------------------------------------------- by kriswallsmith at 2012-01-11T20:12:34Z I personally consider this a coding standard but don't want to bikeshed here :) --------------------------------------------------------------------------- by vicb at 2012-01-11T20:28:08Z I have [tried](https://gist.github.com/1596588) at home. `strpos ` **is** faster unless you have a very long string, probably because you do not need to create a new string, interesting, thanks for the tip. --------------------------------------------------------------------------- by Tobion at 2012-01-11T22:40:18Z I think strpos() is more useful. Say you want to change the string you have to replace 2 variables (the text and the length parameter) when using substr(). It could also introduce bugs when they don't match. With strpos() it's only the text. --------------------------------------------------------------------------- by robocoder at 2012-01-11T22:43:22Z alternate micro-optimization that doesn't create a temporary string: ``` strncmp($v, "@", 1) === 0 ``` --------------------------------------------------------------------------- by Tobion at 2012-01-11T22:47:12Z @robocoder probably the fastest solution but needs to be benchmarked
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|