Sometimes it seems like everyone has a different opinion on something, and the best function for comparing simple strings in PHP is one of those things. The four primary ways are ereg(), preg_match(), strstr(), and strpos(). For a simple comparison, any of these will work, but, once you start doing a great deal of string comparison, the speed of these functions comes into play. Even the PHP site suggests preg_match() on the ereg() page:

Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

Then, if you check the preg_match() page, it says:

Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

I decided the best answer was to test each one and see what I came up with.

My test was to search for a five character string within a string of approximately 60,000 characters. The test was originally going to run 10,000 times, but I ended up doing that 25 times for each function, taking the average in order to try to limit variation.

Results
ereg .956
preg_match .050
strstr .222
strpos .033

Occasionally, preg_match() would come very close to beating strpos(), but overall the results showed strpos() to be the fastest, followed closely by preg_match(), then strstr(), and pulling up the rear was ereg(). It looks like strpos() is definitely the best function for a quick check, and preg_match() is the answer for any searches that require a little more sophistication, but the differences were minimal. Keep in mind, these results represent 10,000 searches of a nearly 60,000 character string. Most applications/scripts would never come close to that, so the function choice isn’t a major concern.


7 Responses to “The Speed Of String Comparison Functions In PHP”

  1. 1 Yogesh Suryawanshi

    This is the great article very useful for writing optimized code.

  2. 2 Markus

    Thanks for the results.

    Which PHP version was used for this test?

  3. 3 Ian Clifton

    I can’t say for absolutely sure since I didn’t make note of it at the time (but I definitely should have!), but I believe it was 5.2.0, since that was the most current version at the time.

  4. 4 Anders Heie

    You may see differente results for many small searches, since the time it takes to run each function probably is time = setup constant + search time.

    It would be interesting to see the results summarized for various search lengths, particularly searching short strings.

    Thanks for running the test and posting it !

  5. 5 Dudewtf

    Do you know whats the average speed of preg_replace?
    Is it right up there with preg_match? preg functions might be slow because the it scans the string several times depending on how many () you have in the regex.

  6. 6 Ian Clifton

    Hi Dudewtf, I haven’t specifically tested preg_replace in a controlled environment, but I’d suspect it is only slightly slower than preg_match. Finding matches is the hard part; after that, it’s just string replacement. Assuming your string isn’t excessively long, the operation is fairly trivial.

  1. 1 PHP: speed of string comparison functions | Tummblr

Leave a Reply