Mark’s Site

Pensieve for coding and golf :-)

Performance Friendly PHP

By admin • Jun 12th, 2008 • Category: 1.3. Good Practice Coding

As you grow into programming, you can pick up some poor habits along the way. These habits produce the same output, but when used in large projects could end up causing some performance headaches in the long run, causing you to get mentally spanked for not paying attention when reading this article!

So to save your ass, I’d like to shed some light on good versus bad methods. You may disagree with some methods because they may not improve readability of code, and that is your choice, but the ultimate aim of this article is to promote the most performance friendly method for a given situation.

Strings on Multiple Lines

If you work with PHP, sooner or later you will need to manage a multiple line string in code (whether it be for outputting text, or creating a SQL statement string). If you need to create a multiple line string, my advice is to use multiple lines within the ONE string.

Eg: Don’t do this:

<?php
$sql_insert_string = 'SELECT id, name';
$sql_insert_string .= 'FROM products';
$sql_insert_string .= 'ORDER BY name ASC';
?>

(performance unfriendly: PHP has to allocate memory for three strings and concatenate them)

Or this:

<?php
$sql_insert_string = 'SELECT id, name'
. 'FROM products'
. 'ORDER BY name ASC';
?>

(performance unfriendly: Even though this looks like one long string, PHP is really dealing with three strings and has to concatenate them to form the one string)

Instead, do this:

<?php
$sql_insert_string = 'SELECT id, name
FROM products
ORDER BY name ASC';
?>

(performance friendly: PHP has one string to deal with, single memory allocation, no concatenation)

Use Single Quoted Strings

I’m sure you know about PHP’s nice and friendly way of allowing you to put variables directly inside double quoted strings like this:

<?php
$years_old = 21;
$one_liner = "Hey good lookin, you must be $years_old this year";
?>

(performance unfriendly: unnecessary overhead)

This seems nice and handy I know, but for PHP, this means that ANY string with double quotes has to be searched for potential variables = unnecessary overhead.

Instead, stick to the single quote method. As I also work with VB.NET (where there is no double quote support for strings) this wasn’t a hard one for me to adjust to, but if you’re starting out, it’s important to not get into the bad habit of using double quoted strings if you can avoid it. You might think the overhead marginal, but add up all the double quoted strings in your ever-growing application, put your application under heavy load with shitloads of users hitting your webserver and you’ll be wishing you had written your strings in the single quote method.

This is the better version:

<?php
$years_old = 21;
$one_liner = 'Hey good lookin, you must be '.$years_old;
?>

(performance friendly: No double quotes here, yay!)

I can attest to this being true. I had a project a year or so ago that dealt with heavy mathematical operations (luckily the mathematical operations were already written and I just had to translate them to PHP). I had the bad habit of always writing variables within double quoted strings for various output. When it came time to analyse the performance, the client wanted every minisecond he could get. I had to find ways to knock a few seconds off execution time. The first thing I did was a mass find-and-replace operation to convert double quoted strings to the single quote method (not an easy operation), but when I was done we shaved a second off. That one second proved to me just how important this method is. Imagine an extra second load time for users when your webserver is under load :-/

Note: This same principle applies to array indexes:

<?php
//if using string indexes, do it like this:
echo $date_array['january'];
//not like this:
echo $date_array["january"];
?>

String Concatenation and Output

This is a pretty cool trick I didn’t know about until recently, but you can use commas to concatenate strings in PHP and it provides a performance benefit.

For example:

<?php
//while this looks like a single line of output:
echo 'SELECT id, name' . ' FROM products' . ' ORDER BY name ASC';
//to PHP it actually translates to:
echo 'SELECT id, name';
echo ' FROM products';
echo ' ORDER BY name ASC';
?>

(performance unfriendly: even though this looks like one long string, PHP is actually dealing with three separate strings here, and sending all three strings to output as shown in the translated section …hence unnecessary overhead)

Instead, use commas:

<?php
//this looks like a single line of output:
echo 'SELECT id, name', ' FROM products', ' ORDER BY name ASC';
//it actually translates to one line of output for PHP:
echo 'SELECT id, name FROM products ORDER BY name ASC';
?>

(performance friendly: PHP has one string to deal with, single memory allocation, no concatenation)

Choice of Functions

Always refer to the official PHP manual when confronted with a choice of potential PHP functions for a given situation. Many functions exist that appear to do the same thing, but if used unnecessarily can lead to unnecessary overhead/confusion in your applications.

For example, you will probably get some advice to look at using preg/ereg functions in PHP whenever you ask about string replacements, but if you need to do a simple string replacement (Eg. replace all ‘http’ with ‘https’) use the str_replace function. Only use the preg/ereg functions when you need to involve regular expressions …that is their purpose. And str_replace is for simple string replacements.

Tagged as: , , ,

admin is
Email this author | All posts by admin

Leave a Reply