RSS

PHP : Optimizing

Thu, Sep 25, 2008

PHP

So you have a huge site with multiple PHP Scripts and you get a load of traffic all of a sudden, when you built the site you didn’t figured 0.01 second load time was fine right? Well, say you get 5,000 visitors daily; that 0.01 can turn into 50 seconds!!!

Don’t know how to benchmark your script? It’s easy:

$start = microtime();
//your script here
$end = microtime();
$time = $start – $end;
echo(round($time, 3));
//your script here
$end = microtime();
$time = $start – $end;
echo(round($time, 3));

Now, lets turn that frown of yours upside-down.

Echo is faster than Print (marginally, but it all adds up)

Benchmark:
Print:
0.000314
0.00031
0.000387

Echo:
0.000177
0.000168
0.000235

Unset variables and arrays (especially); this will only help load speeds if your servers ram is full and strangling it, though it is still a good practice to unset variables and arrays when you no longer need them.

Use full paths in Include and Requires, this allows the server to be a zombie; and not have to resolve the filepath constantly.

Benchmark
include(“file”)
0.014752 (omit)
0.000335
0.000331
0.000327

include(“./file”)
0.000179
< 0.000001 (omit)
0.000117
0.000111

Don’t limit yourself to just Logic statements, use select() for statements that would better suit it, don’t just assume that if/then/else will cover it all, because it might; but it can become slow if you have a huge list of redundancies.

Suppressing errors with the @ sign can be extremely slow. Instead use error_reporting(0)

Benchmark:
@include(“file-does-not-exist.php”)
0.000241
0.000243
0.000238
0.000251

error_reporting(0);
include(“file-does-not-exist.php”)
< 0.000001
< 0.000001
0.000122
0.00012

Close DB connections when you no-longer need them, this is along the same lines as unsetting variables; but will give your database some breathing room.

Calling arrays with single quotes is much faster than no quotes ($array['1'] is faster than $array[1])

Benchmark:
No Quotes
0.000128
0.000136
0.000137
0.000133

Single Quotes
< 0.000001
< 0.000001
< 0.000001
< 0.000001

Globals are marginally slower than variables (only noticeable in duplicate applications).

Benchmark:
echo($_SERVER['REMOTE_PORT']); (x5)
0.000173
0.000785
0.00017
0.000169

$var = $_SERVER['REMOTE_PORT'];
echo($var); (x5)
< 0.000001
0.000157
< 0.000001
< 0.000001

Use single quotes for strings instead of double quotes (if you use double quotes, PHP searches for strings inside the string)

Benchmark:
Double:
0.000146
0.000149
0.000155
0.00015

Single:
< 0.000001
< 0.000001
< 0.000001
< 0.000001

Use static HTML where possible, PHP can be up to TWENTY times slower than HTML. This is due to the fact that PHP compiles the script every time you call it, go learn how to cache the files. If you run a dedicated or VPS server, you will want to ask your host about ZendOptimizer for your PHP Installation. You can check to see if you already have it: php_info your server and CTRL+F for “Zend” and it should bring up it’s own block of info. If it’s installed, you should have at least basic caching setup.

$add++ is slower than ++$add

Benchmark:

$var++
0.00032
0.000314
0.000319
0.000273

++$var
0.000161
0.000173
0.00018
0.00012

Don’t make everything in OOP, as this will end up just adding overhead, use OOP for things that should be in OOP, and nothing more.

Use the default PHP functions where possible, no sense in making your own function that does the same thing as a pre-defined default function.

For those of you that have servers buckling under the stress of traffic, you may consider taking all PHP out of your site’s pages and making different scripts that are run once nightly by a cronjob that will then build the HTML files for your site. This keeps the site dynamic, but also keeps the server from dropping to it’s knees every time a page is loaded by a visitor.

Also: If you believe my benchmark’s are skewed because I ran them at the same time, you will be happy to know that I ran the slower methods before running the faster methods. I’ve also put (omit) next to each of the results that seem to have hit extreme peaks or dips in performance.

,

1 Comments For This Post

  1. Stone Deft Says:

    Well optimizing php is a broad topic. Using php functions instead of making your own functions is a sound advice but a better advice will be integrating all your needed php functions into one function and require or include it once in your index file that way all the functions you need will be active in all the the pages php dynamically creates.

Leave a Reply