PHP
  Home arrow PHP arrow Page 4 - A Few Tips for Speeding Up PHP Code
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

A Few Tips for Speeding Up PHP Code
By: Daryl Houston
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 41
    2003-10-02

    Table of Contents:
  • A Few Tips for Speeding Up PHP Code
  • Filter Database Results
  • Consolidate Queries
  • Pattern Matching Metrics
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Few Tips for Speeding Up PHP Code - Pattern Matching Metrics


    (Page 4 of 5 )

    This is a hard one to test accurately because running a single instance of a pattern match takes negligible time that's difficult to separate from network latency. For example, in testing four different pattern matching functions, I found that none consistently took more than about 40 milliseconds to execute a single pattern match, even within a string of 20,000 or 30,000 characters. It's hard to chalk that up to sluggishness of a given function rather than to network or server load issues. The following table shows the functions I tested and the time they took to process:

    <table border="1" cellpadding="3" cellspacing="0">
     <tr>
      <td align="left" valign="top"><b>ereg()</b></td>
      <td align="left" valign="top"><b>preg_match()</b></td>
      <td align="left" valign="top"><b>strstr()</b></td>
      <td align="left" valign="top"><b>strpos()</b></td>
     </tr>
     <tr>
      <td align="left" valign="top">35.1 ms</td>
      <td align="left" valign="top">35.31 ms</td>
      <td align="left" valign="top">35.58 ms</td>
      <td align="left" valign="top">32.79 ms</td>
     </tr>
    </table>

    They're all pretty close. But when I do a lot of pattern matches within a script, rather than just the one instance, the function I use does seem to make a little bit of a difference. Consider the following test code:

     if(!$_GET["type"]){ print "Must specify type ereg, preg_match, or strstr."; exit; }

     //Generate a long string to perform matches against.
     for($j=0; $j<5000; $j++){
      $x .= "test " . $j . " -- ";
     }

     for($i=0; $i<10000; $i++){
      //For 10,000 iterations, run the specified function,
      //searching for the string "st 10".
      switch ($_GET["type"]){
       case "ereg":
        $y=ereg("st 10",$x);
        break;
       case "eregi":
        $y=eregi("st 10",$x);
        break;
       case "preg_match":
        $y=preg_match("/st 10/",$x);
        break;
       case "strstr":
        $y=strstr($x,"st 10");
        break;
       case "strpos":
        $y=strpos($x,"st 10");
        break;
       default:
        print "Must specify type ereg, preg_match, or strstr."; exit;
      }
     }

    The results of this test are as follows:

    <table border="1" cellpadding="3" cellspacing="0">
     <tr>
      <td align="left" valign="top"><b>ereg()</b></td>
      <td align="left" valign="top"><b>preg_match()</b></td>
      <td align="left" valign="top"><b>strstr()</b></td>
      <td align="left" valign="top"><b>strpos()</b></td>
     </tr>
     <tr>
      <td align="left" valign="top">1442.76 ms</td>
      <td align="left" valign="top">252.22 ms</td>
      <td align="left" valign="top">542.96 ms</td>
      <td align="left" valign="top">297.43 ms</td>
     </tr>
    </table>

    This really didn't accord at all with my expectations. I had expected preg_match() to run the slowest because it has to load and run perl's regular expression engine. And I had read that it was slower than eregi, which I expected to be the fastest of the functions. I ran these tests a number of times to verify that the time discrepancy wasn't merely a fluke, and the results were similar each time. So it appears, oddly, that if you're doing a quick pattern match, it may not matter which of these functions you use, but if you're doing many searches, preg_match or testing for a true return from strpos() might be your best bet. This is particularly heartening in that preg_match is more versatile, allowing for much more complicated matches than any of the other functions.

    I also tested ereg_replace() and preg_replace() for speed. Again, contrary to my original expectations, preg_replace() proved faster in both the single-replacement test and in 100 iterations of a block of test code very similar to that given above.

    <table border="1" cellpadding="3" cellspacing="0">
     <tr>
      <td align="left" valign="top"> </td>
      <td align="left" valign="top"><b>ereg_replace()</b></td>
      <td align="left" valign="top"><b>preg_replace()</b></td>
     </tr>
     <tr>
      <td align="left" valign="top"><b>One Run</b></td>
      <td align="left" valign="top">50.16 ms</td>
      <td align="left" valign="top">37.42 ms</td>
     </tr>
     <tr>
      <td align="left" valign="top"><b>100 Iterations</b></td>
      <td align="left" valign="top">1383.75 ms</td>
      <td align="left" valign="top">159.75 ms</td>
     </tr>
    </table>

    I can't help thinking I've overlooked some condition in my testing, as I've always read that the preg functions were slower than the ereg functions. Based on these metrics, however, I've concluded that the preg functions may be the best to use, given their flexibility and apparent superior speed.

    More PHP Articles
    More By Daryl Houston


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT