Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Optimizing Your PHP Scripts
 
Article Index
Optimizing Your PHP Scripts
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
Page 11
Page 12

As you can see, method #2 (preg_match()) was approximately 30% faster than the comparable ereg() method. In general, you will find that preg_* functions are always faster in text processing than their ereg() counterparts.

Although sometimes regular expressions are the only reasonable method of parsing and processing text, many times nonregular expression solutions are considerably faster than either regular expression flavor. This is particularly true when attempting to find or replace string constants. To illustrate this, let's look at the profiles of preg_match() and strstr() to count the number of strings that contain the substring jjj.

For method one, we'll use a regular expression and the preg_match() function similar to that found in the previous example:

for($i = 0; $i < 10000; $i++) {
if(preg_match("/.*jjj.*/i", $strings[$i])) {
$found++;
}
}

For method two, we'll use the strstr() function (which finds a constant substring within a string):

for($i = 0; $i < 10000; $i++) {
if(strstr($strings[$i], "jjj")) {
$found++;
}
}

Profiling these two methods, we find the following:

Method one took 0.11128091812134 seconds.
Method two took 0.05986499786377 seconds.

Method two was faster than Method one by 46.20%

Obviously, with a 46% performance increase against the faster of the two regular expressions, it is strongly recommended that the standard PHP string manipulation functions be used whenever possible.



Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 131
Statistic


Last Post

 
Top! Top!