View Single Post
Old 09-30-2007, 05:15 PM   #4 (permalink)
jordie
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Good call! Using your exact same code and looping 1 million times, I pitted eregi against preg_match with the case insensitive switch set. I already have a sneaky feeling preg_match is faster, but we shall see.

This will be based on the following simple code:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
    
var_dump(preg_match('/^[A-Z\.]+$/i'$myString));

And:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
var_dump(eregi('^[A-Z\.]+$'$myString));

Here are the results:
  • eregi #1: 10.83 seconds
  • eregi #2: 10.41 seconds
  • eregi #3: 10.29 seconds
  • preg_match #1: 10.42 seconds
  • preg_match #2: 10.15 seconds
  • preg_match #2: 10.06 seconds

Surprisingly, not a whole lot of difference. It may be that preg_match has the advantage when complex regular expressions are being used. preg_match is marginally faster than eregi and I'll think I'll be sticking with it as my preference!
Thats very interesting. I too thought preg_match would be a lot faster, so i ran your tests as well. To my surprise, I found that I got a second faster for eregi. However, this is running PHP 5.2 on my local machine (so a windows platform and apache server, but its a month old PC and is a fairly decent machine with dual core, so not sure if that comes into play at all).

preg_match: 2.83 seconds
eregi: 1.95 seconds

When I ran the same test on my webserver, I got completely different results. This is PHP 4.4.7 on Linux and the results were a second faster for preg_match. So the complete opposite of my local test.

preg_match: 1.21 seconds
eregi: 2.47 seconds

I also ran my previous speed test on my webserver, but that had no change in the results.

So are the speed of regular expressions dependent on what OS they're on? Or is more to do with which PHP version? I'd like to get more tests on this to find out. Which PHP version & OS did you run your test on?
jordie is offline  
Reply With Quote