The best way to get significant speed-ups to your site is to unpublish and uninstall all the modules, mambots and components you do not really need, then compress your images and check if your HTML validates.
But to get those extra percentages of performance, you’ll need to do some code optimization. Optimized code is especially important on websites with heavy load.
PHP is a very fast programming language, but there is more to optimizing PHP than just speed of code execution. PHP involves many factors, which are not code related. Tuning PHP requires an understanding of how PHP performs in relation to all the other subsystems on your server, and then identifying bottlenecks caused by these subsystems and fixing them. The most usual bottleneck is the SQL server, that’s why you should always try to use as fiew query’s as possible. The Joomla devs has done a great job reducing the number of query’s.
When it comes to optimization there is also a set of tradeoffs between scalability and speed. I’ll use an example.
Suppose we need to write a PHP script that reads a 250K file and generates a HTML summary of the file. We write 2 scripts that do the same thing: sprinter.php that reads the whole file into memory at once and processes it in one pass, and marathon.php that reads the file, one line at time, never keeping more than the longest line in memory. marathon.php will be slower as multiple reads are issued, requiring more system calls.
sprinter.php requires 0.04 seconds of CPU and 10 Mb RAM and marathon.php requires 0.06 seconds of CPU and 5 Mb RAM. The server has 100 Mb free actual RAM and its CPU is 99% idle.
At 10 concurrent scripts running, sprinter.php will run out of memory (10 x 10 = 100). At that point, marathon.php will still have 50 Mb of free memory. The 11th concurrent script to run will bring sprinter.php to its knees as it starts using virtual memory, slowing it down to maybe half its original speed; each invocation of sprinter.php now takes 0.08 seconds of CPU time. Meanwhile, marathon.php will be still be running at its normal 0.06 seconds CPU time.
Assuming that a typical PHP script completes in 0.1 seconds and the Internet latency is 0.2 seconds, only 33% of the 0.3 seconds response time that the HTTP client sees is actual PHP computation. So if you improve a script's speed by 20%, the HTTP client will see response times drop to 0.28 seconds, which is an insignificant improvement. Of course the server can probably handle 20% more requests for the same page, so scalability has improved.
Now over to some simple optimizations you can do to your components, modules and mambots or any PHP script for that matter.
First let me show you how I benchmarked the PHP code. To get my benchmarks as accurate as possible I run each set of code 1000 times and I run the script 10 times, 5 times with the code in case #1 and 5 times in case #2, then I take the average and calculate the % of improvement.
Code: Select all
<?php
$loopTimes = 1000;
$before = microtime(true);
for ($i = 0; $i <= $loopTimes; ++$i) {
// case #1:
// Put code to be benchmarked here.
// End of code to benchmark
}
$after = microtime(true);
$case1 = $after - $before;
$before = microtime(true);
for ($i = 0; $i <= $loopTimes; ++$i) {
// case #2:
// Put code to be benchmarked here.
// End of code to benchmark
}
$after = microtime(true);
$case2 = $after - $before;
echo "Case 1 ran $loopTimes times in $case1 seconds<br />";
echo "Case 2 ran $loopTimes times in $case2 seconds<br />";
?>
Case 1:
$string = 'one' . 'two' . 'three' . 'four' . 'five' . 'six';
This is a lot faster than doing
Case 2:
$string = 'one';
$string .= 'two';
$string .= 'three';
$string .= 'four';
$string .= 'five';
$string .= 'six';
When I benchmarked this simple piece of code I got a 40-50% speed improvement in Case 1 over Case 2.
Using single quotes instead of doubles (where applicable) can give you a microscopic speed increase, remember double quotes are parsed for variables, tabs and new line characters, where as singles are not.
When just putting a echo “hello”; and echo ‘hello’; into my benchmark I got a 40% performance boost when using single quotes, even though your overall speed increase will be microscopic it might help a bit if you have 1000’s of echo’s that is unnecessarily double quoted.
There is also a small speed increase when using true over TRUE, and false over FALSE, this is because PHP has to convert TRUE to lowercase to process it, the speed increase here was only 10-12%, if you want to add another 10-12% speed increase use 1 and 0 instead of true and false. But this will hurt the readability of your code, but if it’s a simple script then you should go for 1 and 0 and not true/false.
I’ll not go through a [censored] load of examples in this post, those simple examples where only there to show that even small things matter, and I urge all 3. Party developers to optimize their code a bit more before releasing the code to the public.
To find out what gives the best performance, do a little benchmarking, takes you only 5 minutes extra.
Inside loops is a good place to start your optimization as this code run X number of times, so if your execution time drops 0.1 seconds per loop, then if that loop runs 100 times you’ve saved 10 seconds. Most scripts aren’t that slow, if you don’t run your web server on a Commodore 64 that is.
The reason I wrote this post is that I’ve been able to get a speed increases of about 400-500% from some of the 3. party modules and mambots downloaded from the extensions section, which again points to poor optimization. So please do a bit of benchmarking while developing your modules, components and mambots. Using the microtime() function is the easiest way to do benchmarking.