mardi 31 mars 2015

Fastest way to search for string within a string, given a set of constant factors

I see there are a lot of questions on the topic, but please bare with me.


I have a long string, in which I need to determine whether there is an occurrence of a short string. It sounds very simple, and it is, using strpos I can perform



strpos($longstr, $shortstr);


Which would get the job done, however there are some facts that I know for certain:



  • the short string will always start on a new line

  • the short string will always be 10 characters long (multibyte)

  • the short string will always start with the characters "чь"

  • the short string will always end with a new line after the 10th character

  • the short string can only occur once, further occurrences are meaningless and disregarded


Having the above conditions in mind, it makes no sense to search for the string in question in any position other than 0 on a new line, however, how can I know where the new lines are if I don't go through the whole long string.


Is there anything that can be improved in getting the job done, performance-wise, compared to strpos?


Aucun commentaire:

Enregistrer un commentaire