corner image corner image
corner image corner image
corner image corner image
corner image corner image
corner image corner image
corner image corner image
corner image corner image

Optimizing your website

One of the most important factors that will lead to visitors leaving is slow loading pages. And at that point, we should make distinction between sveral types of sluggishness: A. Slow due to HTML bloat B. Slow due to long server processing time C. Slow due to long browser rendering time.

We will tackle them one at a time. The first case is Slow due to HTML bloat. Users like pages full of information. HTML bloat happens when we keep modifying the pages and forget that every single addition adds to the time of transfer as well. The first optimization is usually to separate code (HTML) from styles (CSS) and from javascript code. So you should have the HTML data separate by itself. This will help if your jaavscript and css are static files, so the browser will only transfer them if they were modified either by issuing the HTML HEAD or a conditional GET to your web server. The second optimization is turning on php’s buffer compression. In browsers supporting HTTP 1.1 the server can respond with a compressed version of the file. This will reduce the time of transfer since most transfered files are actually text.

The second type of sluggishness is due to long server processing time. When we talk about server processing time we have to focus on modifications that will lead to significant improvements. Almost all the time is usually used in database queries. The time consumed by the PHP scripts is usually around 5% of the whole time needed to display a page. You can easily figure this out by using a timing function such as the very popular getmicrotime:

function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}

I added this function in my beta version of the website, and ran it at the header and footer as well as inside a query & fetch_array wrappers that are used to access sql results. The result was focusing on optimizing database queries will significantly improve the page load time and will be more fruitful than spending time optimizing the PHP code.

The easiest way to optimize the SQL queries is by letting the website work in the normal mode, and every time your query wrapper is called append a text file with the sql query. By looking at the sql log I noticed a great deal that can be improved including queries that get repeated, un-necessary queries where the calling function can pass the result to the called function without re-querying the database. The most effective optimization is usually minimizing the inserts, updates and deletes. Those are usually the most time consuming. In certain cases one can either run the update or delete in background using delayed inserts in mysql or pg_send_query in and execute it as the last SQL statement in the script.

Your second step is optimizing the PHP code itself. This will only be effective if the time consumed by the PHP code is at least 20% of the whole time, otherwise in my experiments I really didn’t feel any difference. You can optimize the PHP code by splitting large include files into smaller ones and using them only when needed. Use faster functions like str_replace instead of ereg_replace. Use single colon strings with apostrophe’s instead of double quotes when no variable are inside of the string.

The third type of sluggishness is due to long browser rendering time. This happens when the page contains a lot of elements whose size (widths and heights) depend on images to be loaded or further elements in the HTML code. So the browser keeps moving elements around every time it loads a new image or a new HTML element. I regard this as a usability issue other than being a major user nuisance. In some the website visitor will see an image and will move the mouse to click on it only to find it moving away because another element got loaded. To improve on this one should include the widths and heights of most elements even if it was in percentages and not in absolute pixels. This will give the browser the chance to size the element and avoid moving elements around as it is loading and rendering the HTML.

Technorati Tags: , , , , , , ,

Tags: , , , , , , ,

corner image corner image

corner image corner image

One Response to “Optimizing your website”

  1. Jason Meiers Says:

    Yeah it makes sense that alot of the time is spent on database queries. Also for composite applications to pin-point the problem a correlataion between the transaction and the database, application, network and os helps.

Leave a Reply

corner image corner image
1,372 spam comments
blocked by
Akismet