SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Optimizing your PHP scripts

    Hey,

    I've been working on my very first PHP page. (yep, im no longer a virgin) Anyway, there is a lot of code. I'm about 80% done and have 200 lines of code. Now, besides that. What's a good way to optimize your pages? I just want to be prepared for heavy traffic, if it happens. Also

    Is the PHP going to slow a page down, or the MySQL database. Both need to be optimized.

    Is there a site that can test your page? ok, thanks guys.

    -Brandon

  2. #2
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there any portable sections in your code? Could you perhaps include (inc.php) some sections that are redundant like a footer or header or DB connection? I think at the end of the day the length of code doesn't really matter, it's the effectiveness of you going back and making some changes and being able to identify what script does what...

    I'm new to php too (been around html and other codes for a while) so I'll be checking into this thread to see what others are saying.

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To make an optimal code you have to plan it well before you begin coding. It's not a right time to look back to optimization when the script is nearly finished. Every time I have to optimize a script it comes to a complete rewriting. I hate doing that so I try to plan things well before I even start thinking about it. That's not easy though, especially when you're short on time, but the skills come with the experience.

    Just my 2 cents.
    Saul

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah dont worry about the length of the code, thats rarely going to net you much in terms of speed.

    optimizing your database, and your querys will net you potentially large increases in speed.

    you can time your scripts execution fairly easily, or if you like, you can time just parts of your script.


    take a look at the manual for this function, they give examples of how to use it to time your scripts.
    microtime()

  5. #5
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When looking to optimize code you've been given, the only real way is to walk through it and understand well what the computer is doing. Then you can see any redundancies or major time hogs.

    First, pay the most attention to the most inner loop, which is executed the most of all code, and "slow" operations, like file input/output.

    Are you including functions or files that you aren't using?

    Can you break up the page's functions into groups of functions in different include files so that, for example, you only include the functions/classes used for printing a customer's shopping cart when the customer is viewing the cart?

    Are you taking into account if statement short circuiting? Example:
    PHP Code:
    foreach ($largearray as $element) {
        if (
    eregi($one$two) && $is_ok) {
            
    do_something();
        }

    In this example, to get to do_something(), $is_ok must be true and the regular expression must match. It is neglegible whether or not $is_ok is true, but the call to eregi() can be computationally intensive. If the two expressions were reversed, the interpreter would check $is_ok first, and if it is false, there's no need to call eregi(), so the interpreter would skip it and the execution would run faster. The same can be applied to if ($is_ok || eregi($one, $two)): If $is_ok is true, there's no need to call eregi().

    You can measure how much time different parts of the code take to execute:
    PHP Code:
    $start_time microtime(true);
    //The code you're measuring.
    echo 'Code executed in ' number_format(1000 * (microtime(true) - $start_time), 2) . ' ms.'
    If you have some problematic code, you can post some of it and we can point out any issues we see.

    Cheers!


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
  •