SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    virginia
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    General tips for writing lean fast php code

    I have a feeling that the php code for a site I'm working on is very slow. It seems like its taking a few seconds for my host to process the script and thats just with one person browsing. Are there any general tips out there for writing lean fast code.

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    India
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out this post:

    PHP and MySQL coding tips

  3. #3
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Some general tips:




    1. narrow database queries scope: instead of doing Select * from someTable do Select * from someTable Where xxx or Select * from someTable Select * from someTable Limit 5, 10. If you have even just one query of an entire table it can kill the speed of you application; or many non-narrowed queries can also lead to slow results
    2. If using classes, include them when they are needed rather than loading classes that are not used for the current execution.
    3. Use single quotes whenever possible (rather than double quotes)
    4. Employ Chunked Buffering to Cache for a long time the parts of your pages that rarely change, and regularly update areas of frequent use by having short caching periods. Rather than writing your own caching engine you might consider using PEARS Cache_Lite class.
    5. Write code so that you have only the minimum amount of code to service the job. Look for places where tasks are being repeated and roll functions to reduce the scope of code that needs traversing. For example write functions that are polymorphic or use Object Orient Programming (OOP) and write classes that utilize Inheritance, Polymorphism, Aggregation, and Composition. By combining OOP with Caching you can write less code and have good performance. For a great case on why to use OOP read this http://www.sitepoint.com/forums/showthread.php?t=59898 thread. little plug for OOP.
    6. Don't do things like this:

    PHP Code:
    for ($i 1<= COUNT($Units); ++$i){
                    echo 
    "$i";
      } 
    Instead do this:

    PHP Code:
      $count COUNT($Units);
       for (
    $i 1<= $count; ++$i){
                     echo 
    "$i";
       } 
    calling functions in loops again and again can be costly on speed and memory. When possible try to re-write loops such that the any static values required for the loop's evaluation are determined before the loop is run.

    Hope some of this helps,
    ServerStorm

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Venlo, the Netherlands
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding SQL

    Select * from someTable
    Rather then doing select *, only specify the columns you really need:
    SELECT column1, column2, column3 FROM sometable

    Never nest mysql queries. So do NOT do this:
    PHP Code:
    $sQuery 'SELECT id FROM table1';
    $rsResult mysql_query($sQuery);
    while(
    $aRow mysql_fetch_assoc($rsResult)){
      
    $sQuery 'SELECT column1, column2, column3 FROM table2 WHERE id = '.$aRow['id'];
      
    // etcetera

    Also: instead of performing multiple queries, try using joins (on well-indexed tables)

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joflow
    I have a feeling that the php code for a site I'm working on is very slow. It seems like its taking a few seconds for my host to process the script and thats just with one person browsing. Are there any general tips out there for writing lean fast code.
    Well generally there is no "general" tips. You'll need to profile your application first to find out what exactly slows it down.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •