PHP Code Optimization, 3. party devs, please do it.

Discussion regarding Joomla! Performance issues.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Security and Performance FAQs
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
Masquerade
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Tue Apr 04, 2006 10:36 pm
Contact:

PHP Code Optimization, 3. party devs, please do it.

Post by Masquerade » Sat Aug 19, 2006 5:32 pm

If you ask me, Joomla is one of the best CMS’s out there, it’s fast and secure. But when it comes to 3. Party components, modules and plugins/mambots there is a LOT of crappy code (crappy as in slow) floating around so I decided to put together a fiew tips on optimizing your PHP code to run a bit faster. I’ve seen a lot of users complaining on the forums that their site has a lag, especially when using a lot of modules and mambots. The longer it takes the PHP scripts to run the longer it will take PHP to generate the HTML.

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 />";
?>
The first test I did have to do with strings. OK, lets start with a simple string example.

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.

User avatar
nekkarcity
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 109
Joined: Thu Aug 18, 2005 6:07 am
Location: Tettnang - Germany
Contact:

Re: PHP Code Optimization, 3. party devs, please do it.

Post by nekkarcity » Tue Aug 22, 2006 4:56 pm

Very nice, Thanks for share it! 8)

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24985
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: PHP Code Optimization, 3. party devs, please do it.

Post by pe7er » Tue Aug 22, 2006 5:41 pm

Masquerade wrote: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.
Thank you for sharing this info!
Did you inform those 3rd party extension programmers about their code optimation possibilities?
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

thematrix
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Thu Dec 01, 2005 4:38 pm
Location: Belgium
Contact:

Re: PHP Code Optimization, 3. party devs, please do it.

Post by thematrix » Mon Aug 28, 2006 4:20 pm

The Real problem is that alot of the devs of these components write something to make it work or port something with whatever way they know or can find out on tutorial or other sites.

Many of them aren't experienced programmers with experience in "best practices" for either php or mysql and alot of them are just hacking away at existing code, functions and classes to make it work with Joomla. If they start out with badly written classes and functions available onlin, to start with, their end product will have performance problems thats unrelated to their own code, but in alot of cases they have no idea how to fix it since they didn't create those classes and functions.

Another reason you see very easely for php and mysql performance problems is bloated files and query's and overcommented files.

That is probably the main reason for some modules and components to run like utter crap when run uncached and blazing fast when cached by eA, Zend or APC.

When the files are compiled and stored in the caches the unneeded stuff gets stripped, the code gets parsed and optimized by the cacher.

One of the first things I do is take out all comments out of files when I publish them for a live website.

I've come across instances where 500 bytes of actual code was stuck in a 20KB size file, solely because the file had 19.5KB of code comments, info, spaces, tabs and other useless things in it. Heck, some people find it fun to past ASCII ART logo's into their files to show they created it!

azaull
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Nov 09, 2011 7:30 am

Re: PHP Code Optimization, 3. party devs, please do it.

Post by azaull » Thu Mar 29, 2012 3:27 pm

"PHP is a very fast programming language"
PHP is not a programming language. Remember that !
It is a script language.Programming laguages are : C,C#,C++,VB,Java ......

User avatar
Tonie
Joomla! Master
Joomla! Master
Posts: 16553
Joined: Thu Aug 18, 2005 7:13 am

Re: PHP Code Optimization, 3. party devs, please do it.

Post by Tonie » Thu Mar 29, 2012 4:01 pm

This thread is more than five years old......


Locked

Return to “Performance - 1.0.x”