Optimising code is always remians to be a tough thing when we come across sites having high traffic as website load time is critical value that determines the success of your website.Even though we have so many options or techniques to minimise the speed load issue,Database performance tuning,reducing Bandwidth etc, it still needs more attention for making your websites a Good SEO valued one .
Lets discuss about some optimising techniques and thereby executing codes faster .1.Whether to go for “echo” or “print”
Both commands are appears to be the same thing .
Lets take an example
echo “Hello Computer<br/>”;
print “Hello Computer<br/>”;
echo (“Hello Computer<br/>”);
print (“Hello Computer<br/>”);
Both code snippets will outputs the text Hello Computer in separate lines .
We can differentiate both commands on basis of faster execution.The complete iteration took 450ms for echo whereas for print it would be 550ms.As we can see the difference coming is about 20% and the benefit is marginal, normally echo is the most preferrable one.
NOTE: It is recommended to add strings using parameters rather than using concatenation or multiple echo calls.Using parameters could save a couple of seconds in the long run.
Example using parameters:
echo “Hello” , “Computer” , “<br />”;
2. Avoiding time() or microtime()
In PHP 5.1 http request current time is stored in $_SERVER[’REQUEST_TIME’].
So we can reduce the system calls if we avoid using time() or microtime()
3. Parsing Xml in PHP
When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions
4. Full path in require() and include()
Try to use full file paths on include/require statements . Normalizing a relative file path can be expensive; giving PHP the absolute path avoids the extra step.
5. Built-In Functions Vs. Custom Functions
Since PHP has to take the extra step of interpreting your custom functions, built-in functions have a performance advantage. More importantly, there are a lot of useful built-in functions that you may never learn about if you always default to writing your own.
6. Avoid double quotes for single quotes.
PHP has to do extra processing for a string in double quotes to see if it contains any variables. Concatenation with single quotes is comparitively faster.
7. Enable mod_deflate or mod_gzip
In order to reduce bandwidth for high traffic sites, turn on mod_deflate in Apache v2 or mod_gzip for Apache v1. mod_deflate allows Apache2 to compress files and deliver them to clients (e.g. browsers) that can handle compressed content which most modern browsers do.With mod_deflate, you can compress HTML, text or XML files to approx. 20 – 30% of their original sizes, thus saving you server traffic.
8. Caching scripts
For Dynamic web applications it is always recommendable to Install a PHP Caching product like “memcached” or “eAccelerator” or “Turck MMCache”.
Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load.
eAccelerator stores compiled PHP scripts in shared memory and executes code directly from it.
Turck MMCache is a PHP Accelerator uses optimizations for speed up of scripts execution.
9. Profiling the Code
By profiling the code it is easy to identify which part of our code consumes more time. Ex . Xdebug debugger, Advanced PHP Debugger(APD).
10.Comparing str_replace with preg_replace
str_replace will be faster in simple situations when compared to preg_replace .But while dealing with regular expressions using preg_replace is more efficient.
11. Avoid using “@” for Suppressing Error
Using “@” will slow down the code when there is not an error.
While using “@” 2 ini calls, one for setting the error_reporting to zero and another for restoring it to original value is happening.Even in the case of no error too this 2 calls would be executing.This will slow down the code.Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads. Consider having error_reporting(E_ALL) always on.
Try to use PNG image format in your websites as it requires less
size.Image formats with PNG can be compressed 50% of its original size without quality loss.For transparent background use GIF images.JPG or JPEG are
good only for high quality images
Always use minimum quality image that high quality ones or image
with larger size.
By indicating the height and width of images, browser will process
For speeding up pages for repeat visitors it is always recommendable to use static images.
13. Isset() vs Strlen()
Use isset() where possible in replace of strlen() as it is 3 times faster than strlen(). isset() is a language construct and not a function meaning that it’s execution does not require function lookups.
14. When using header redirect
When using header(‘Location: ‘.$url), remember to follow it with a die(),As the script continues to run even though the location has changed or avoid using it all together where possible.
15.Dealing with MAGIC FUNCTIONS/METHODS
PHP has got some set of functions starts with __.These functions are called Magic Functions or Magic Methods.You should not create any function in your application with name starts from __ as __ is reserved for php magic functions .Its hard to do anything useful inside a magic method, such as __get or __call that isn’t 10 to 20 times slower than the “non-magic” solution.
16.DataBase Optimising Techniques
(i) Storing Images
Anything that goes in and comes out of the database is going to have to be transferred to the web browser.So storing images in the database is bad idea.It is recommendable to store images as a file and store the URI in the DB.
(ii) Optimising Queries
1. Avoid doing SQL queries inside a loop.
2. It is faster to run queries like this
INSERT INTO users (first_name,last_name) VALUES(“John”,”Doe”),(“Jane”,”Doe”)
than to run two queries like this.
INSERT INTO users (first_name,last_name) VALUES(“John”, “Doe”)
INSERT INTO users (first_name,last_name) VALUES(“Jane”, “Doe”)
3. If you are inserting a lot of rows from different clients, you can get higher speed using the INSERT DELAYED statement.The DELAYED
option for the INSERT statement is a MySQL-specific option that is very useful if you have clients that can’t wait for the INSERT to complete.
iii) Make sure your tables are indexed properly
(iv) Use persistent connections to the database to avoid the connection overhead.
- If you can’t use persistent connections and you are doing a lot of new
connections to the database, you may want to change the value of the thread_cache_size variable.
(v) Use INSERT /*!LOW_PRIORITY*/ when you want your selects to be more important.