PHP
  Home arrow PHP arrow Page 2 - 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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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 / 38
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    A Few Tips for Speeding Up PHP Code - Filter Database Results


    (Page 2 of 5 )

    There are several ways to fetch database results, depending on your database server type and your PHP configuration. For example, using the sybase drivers to connect to an MS SQL server, my PHP version will let me use the sybase_fetch_array() function but not the sybase_fetch_assoc() function. Sybase_fetch_array() returns both text and numeric indices for each row returned, however, effectively doubling the data I have to pass around when processing the results. In order to reduce the amount of data I have to work with, I added a function to my database library that lets me choose which type of index I wish to use -- text, numeric, or both. Consider the following code:

     $db=new DB("server","username","password","database","sybase");
     $db->connect();
     $db->usedb("database");
     $db->set_return("both");

     foreach($db->fetch("SELECT TOP 200 * FROM [mainview]") as $row){
         print_r($row);
      print "<br><hr><br>\n";
     }
     $db->disconnect();

    Forget for a moment that I'm using a database library you don't have access to.  It's plain enough that I'm connecting to a database, invoking the "set_return()" function, and then looping through an array of row arrays to print the results. My [mainview] happens to be a view containing some 300 columns. Multiply that times 200 and we're working with a fair amount of data. Now the default behavior of my database class is to return both numeric and text indices, and it consistently takes about 55,000 milliseconds to run this query. Only about 8% of that time is spent actually pulling results from the database; the remaining 92% is devoted to processing the results. But when I invoke 'set_return("text")', the results are somewhat improved.  Total runtime is about 41,000 milliseconds, with 40% of the time spent in retrieving results and 60% processing them; and when I set the return to "numeric," the results are even better: 31,000 milliseconds with a 20/80 split. So what did I do to increase performance?

    I wrote a function within my database library that checks the value of the member variable "return_type," runs through the row results, and returns all numeric or text indices based on the value of that member variable:

     function format_results($rows){
      //$rows is assumed to be a multi-dimensional
      //array composed of arrays of row results.
      if($this->return_type=="both" || !$this->return_type){ return $rows; }
       $newrows=array();
       foreach($rows as $r){
        $vals=array();
        //Get all array keys from the row of row arrays passed to function.
        $keys=array_keys($r);

        //For each key, check return type and set vals[$key] to the appropriate modulo value.
        for($i=0; $i<sizeof($keys); $i++){
         switch($this->return_type){
          case "numeric":
           if($i%2==0){
            $vals[$keys[$i]]=$r[$keys[$i]];
           }
           break;
          case "text":
           if($i%2==1){
            $vals[$keys[$i]]=$r[$keys[$i]];
           }
           break;
           //Default case, just return $rows as it was passed.
          default:
           $i=sizeof($keys) + 5;
           return $rows;
         }
        }
        //Push reformatted single row array onto array of row arrays.
        array_push($newrows,$vals);
       }
      return $newrows;
     }

    The result of adding this function is of course that we spend more time formatting the results before returning them to the calling function, but we spend less time processing the results after the fact. In other words, by weeding out duplicate values once up front, we dodge working with twice the data in any loops in our calling script and we stand to save significant time. If your PHP config is such that you can opt to use functions that will return only numeric or text indices, you may not need to write such a function. The lesson, though, is that you should get only the type of results you'll actually be working with. So if you're using text indices only in your code and you happen to be able to use sybase_fetch_assoc(), be sure you use it rather than using the more top-heavy sybase_fetch_array(). I also benchmarked this on a mysql database with similar results.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway