Powered by Blogger.

Contact

Popular Posts

Saturday 24 August 2013



There are a number of tricks that can make your life easier and help you to squeeze the last bit of performance from your scripts. These tricks won't make your web applications much faster, but can give you that little edge in performance you may be looking for. More importantly it may give you insight into how PHP internals works allowing you to write code that can be executed in more optimal fashion by the Zend Engine.

1. Static methods

If a method can be declared static, declare it static. Speed improvement is by a factor of 4.

2. echo() vs. print()

Even both of these output mechanism are language constructs, if you benchmark the two you will quickly discover that print() is slower then echo(). The reason for that is quite simple, print function will return a status indicating if it was successful or not (note: it does not return the size of the string), while echo simply print the text and nothing more. Since in most cases this status is not necessary and is almost never used it is pointless and simply adds unnecessary overhead.
  1. echo( 'Hello World' );    
  2. // is better than    
  3. print( 'Hello World' );    

3. echo's multiple parameters

Use echo's multiple parameters instead of string concatenation. It's faster.
  1. echo 'Hello'' ''World';    
  2. // is better than    
  3. echo 'Hello' . ' ' . 'World';   
Read more...

4. Avoid the use of printf

Using printf() is slow for multitude of reasons and I would strongly discourage it's usage unless you absolutely need to use the functionality this function offers. Unlike print and echo printf() is a function with associated function execution overhead. More over printf() is designed to support various formatting schemes that for the most part are not needed in a language that is typeless and will automatically do the necessary type conversions. To handle formatting printf() needs to scan the specified string for special formatting code that are to be replaced with variables. As you can probably imagine that is quite slow and rather inefficient.
  1. echo 'Result:', $result;    
  2. // is better than    
  3. printf( "Result: %s", $result );    

5. Single quotes vs. double quotes

In PHP there is a difference when using either single or double quotes, either ‘ or “. If you use double quotes ” then you are telling the code to check for a variable. If you are using single quotes ‘ then you are telling it to print whatever is between them. This might seem a bit trivial, but if you use the double quotes instead of the single quotes, it will still output correctly, but you will be wasting processing time.
  1. echo 'Result: ' . $var;    
  2. // is better than    
  3. echo "Result: $var";  
Even the use of sprintf instead of variables contained in double quotes, it’s about 10x faster.

Read more...

6. Methods in derived classes vs. base classes

Methods in derived classes run faster than ones defined in the base class.

7. Accessing arrays

e.g. $row['id'] is 7 times faster than $row[id]

8. Do not implement every data structure as a class

Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. For this reason, do not implement every data structure as a class, arrays are useful, too.

9. Avoid functions inside loops

Try to use functions outside loops. Otherwise the function may get called each time.
  1. // e.g. In PHP for loop with a count() inside the control   
  2. // block will be executed on EVERY loop iteration.   
  3. $max = count( $array );    
  4. for( $i = 0; $i < $max; $i++ )    
  5. {    
  6.     // do something    
  7. }    
  8.     
  9. // is better than    
  10.     
  11. for( $i = 0; $i < count( $array ); $i++ )    
  12. {    
  13.     // do something    
  14. }  
It's even faster if you eliminate the call to count() AND the explicit use of the counter by using a foreach loop in place of the for loop.
  1. foreach ($array as $i) {  
  2.     // do something    
  3. }  
Read more...

Note: A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.

10. ?> <?

When you need to output a large or even a medium sized static bit of text it is faster and simpler to put it outside the of PHP. This will make the PHP's parser effectively skip over this bit of text and output it as is without any overhead. You should be careful however and not use this for many small strings in between PHP code as multiple context switches between PHP and plain text will ebb away at the performance gained by not having PHP print the text via one of it's functions or constructs.

11. isset instead of strlen

When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using a isset() trick.
  1. if (!isset($foo{5})) { echo "Foo is too short"; }  
  2. // is better than  
  3. if (strlen($foo) < 5) { echo "Foo is too short"; }  
Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.

12. true is faster than TRUE

This is because when looking for constants PHP does a hash lookup for name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups. Furthermore, by using 1 and 0 instead of TRUE and FALSE, can be considerably faster.

13. Incrementing or decrementing the value of the variable

When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.

Additionally,

1. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.

2. Incrementing a global variable is 2 times slower than a local variable.

3. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.

4. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.

Read more ... and more...

14. Replace regex calls with ctype extension, if possible

Many scripts tend to reply on regular expression to validate the input specified by user. While validating input is a superb idea, doing so via regular expression can be quite slow. In many cases the process of validation merely involved checking the source string against a certain character list such as A-Z or 0-9, etc... Instead of using regex in many instances you can instead use the ctype extension (enabled by default since PHP 4.2.0) to do the same. The ctype extension offers a series of function wrappers around C's is*() function that check whether a particular character is within a certain range. Unlike the C function that can only work a character at a time, PHP function can operate on entire strings and are far faster then equivalent regular expressions.
  1. ctype_digit($foo);  
  2. // is better than  
  3. preg_match("![0-9]+!", $foo);  

15. isset vs. in_array and array_key_exists

Another common operation in PHP scripts is array searching. This process can be quite slow as regular search mechanism such as in_array() or manual implementation work by iterating through the entire array. This can be quite a performance hit if you are searching through a large array or need to perform the searches frequently. So what can you do? Well, you can do a trick that relies upon the way that Zend Engine stores array data. Internally arrays are stored inside hash tables when they array element (key) is the key of the hashtables used to find the data and result is the value associated with that key. Since hashtable lookups are quite fast, you can simplify array searching by making the data you intend to search through the key of the array, then searching for the data is as simple as $value = isset($foo[$bar])) ? $foo[$bar] : NULL;. This searching mechanism is way faster then manual array iteration, even though having string keys maybe more memory intensive then using simple numeric keys.
  1. $keys = array("apples"=>1, "oranges"=>1, "mangoes"=>1);  
  2. if (isset($keys['mangoes'])) { ... }  
  3.   
  4. // is roughly 3 times faster then  
  5.   
  6. $keys = array("apples""oranges""mangoes");  
  7. if (in_array('mangoes', $keys)) { ... }  
  8.   
  9. // isset is also faster then  
  10. if(array_key_exists('mangoes', $keys)) { ... }  
Note: However the lookup times don't diverge until you've got a very considerable amount of data in your array. e.g. If you have just 2-3 entries in your array, it will take more time to hash the values and perform the lookup than it would take to perform a simple linear search ( O( n ) vs. O( log n ) )

16. Free unnecessary memory

Unset your variables to free memory, especially large arrays.

17. Specify full paths

Use full paths in includes and requires, less time spent on resolving the OS paths.
  1. include( '/var/www/html/your_app/database.php' );    
  2. //is better than    
  3. include( 'database.php' );   
Read more...

18. regex vs. strncasecmp, strpbrk and stripos

See if you can use strncasecmp, strpbrk and stripos instead of regex, since regex is usually slower.

19. str_replace vs. preg_replace vs. strtr

The str_replace is better than preg_replace, but strtr is better than str_replace by a factor of 4.

20. select vs. multi if and else if statements

It’s better to use select statements than multi if, else if statements.
  1. switch( $name )  
  2.   {  
  3.    case 'aaa':  
  4.    // do something  
  5.    break;  
  6.   
  7.    case 'bbb':  
  8.    // do something  
  9.    break;  
  10.   
  11.    case 'ccc':  
  12.    // do something  
  13.    break;  
  14.   
  15.    default:  
  16.    // do something  
  17.    break;  
  18.   }  
  19.   
  20.   // is better than  
  21.   
  22.   if( $name == 'aaa' )  
  23.   {  
  24.    // do something  
  25.   }  
  26.   else if( $name == 'bbb' )  
  27.   {  
  28.    // do something  
  29.   }  
  30.   else if( $name == 'ccc' )  
  31.   {  
  32.    // do something  
  33.   }  
  34.   else  
  35.   {  
  36.    // do something  
  37.   }  
Read more...

21. Error suppression with @ is very slow

  1. $name = isset( $id ) : 'aaa' : NULL;    
  2. //is better than    
  3. $name = @'aaa';   
Read more...

22. Boolean Inversion

Most of the time, inverting a boolean value is as simple as using the logical ‘not’ operator e.g. false = !true. That’s easy enough, but occasionally you might find yourself working with integer-type booleans instead, with 1s and 0s in the place of true and false; in that case, here’s a short PHP snippet that does the same thing:
  1. $true = 1;  
  2. $false = 1 - $true;  
  3. $true = 1 - $false;  
The same principle can be used any time you want to toggle an integer between two values e.g. between 2 and 5:
  1. $val = 5;  
  2. $val = 7 - $val; // now it's 2...  
  3. $val = 7 - $val; // and now it's 5 again  

23. isset($var, $var, …)

Useful little thing, this - you can check the state of multiple variables within a single PHP isset() construct, like so:
  1. $foo = $bar = 'are set';  
  2. isset($foo, $bar); // true  
  3. isset($foo, $bar, $baz); // false  
  4. isset($baz, $foo, $bar); // false  
On a related note, in case you’re not already aware of this, isset actually sees null as being not set:
  1. $list = array('foo' => 'set''bar' => null);  
  2. isset($list['foo']); // true, as expected  
  3. isset($list['bar']); // false!  
In situations like the above, it’s more reliable to use array_key_exists().

24. Modulus Operator

During a loop, it’s a fairly common need to perform a specific routine every n-th iteration. The modulus operator is extremely helpful here - it’ll divide the first operand by the second and return the remainder, to create a useful, cyclic sequence:
  1. for ($i = 0; $i < 10; ++$i)  
  2. {  
  3.     echo $i % 4, ' ';  
  4. }  
  5. // outputs 0 1 2 3 0 1 2 3 0 1  

25. http_build_query

This function turns a native array into a nicely-encoded query string. Furthermore, this native function is configurable and fully supports nested arrays.

26. <input name="foo[bar]" />

HTML + PHP are quite capable of handling form fields as arrays. This one’s particularly helpful when dealing with multiple checkboxes since the selected values can be automatically pushed into an indexed (or associative) array, rather than having to capture them yourself.

27. get_browser()

Easily get your hands on the users browser-type. Some leave this process to the browser end but can be useful to get this info server side.

28. debug_print_backtrace()

I use this one a lot, print a debug-style list of what was called to get the the point where this function is called.

29. Automatic optimization for your database

Just like you need to defrag and check your file system, it’s important to do the same thing with SQL tables. If you don’t, you might end up with slow and corrupted database tables.

Furthermore, you will probably add and delete tables from time to time. Therefore, you want a solution that works no matter how your database looks like. For this, you can use this PHP script that finds all your tables, and then perform Optimize on every single one. Then a good idea can be to do this every night (or whenever your server is least accessed) with “cron” because you don’t want to delay your surfers to much.
  1. dbConnect()  
  2. $tables = mysql_query("SHOW TABLES");  
  3.   
  4. while ($table = mysql_fetch_assoc($tables))  
  5. {  
  6.    foreach ($table as $db => $tablename)  
  7.    {  
  8.        mysql_query("OPTIMIZE TABLE '".$tablename."'")  
  9.            or die(mysql_error());  
  10.    }  
  11. }  

30. require() vs. require_once()

Use require() instead of require_once() where possible.

Read more...

31. Check System Calls

A common mistake with Apache
  1. /usr/sbin/apache2 -X &  
  2. strace -p 16367 -o sys1.txt   
  3. grep stat sys1.txt | grep -v fstat | wc -l  
  4. ...  
  5. index.html (No such file or directory)  
  6. index.cgi  (No such file or directory)  
  7. index.pl   (No such file or directory)  
  8. index.php ...  
Fix DirectoryIndex
  1. <Directory /var/www>  
  2.     DirectoryIndex index.php  
  3. </Directory>  

32. Secure HTTP connections

You can force a secure HTTP connection using the following code,
  1. if (!($HTTPS == "on")) {  
  2.    header ("Location: https://$SERVER_NAME$php_SELF");  
  3.    exit;  
  4. }  

33. Avoid magic like __get, __set, __autoload

_get() and __set() will provide a convenience mechanism for us to access the individual entry properties, and proxy to the other getters and setters. They also will help ensure that only properties we whitelist will be available in the object. This obviously carries a small cost. Also, __autoload will affect your code more less in the same way as require_once.

References

[1] Here it is. 8 randomly useful PHP tricks. http://theresmystuff.com, 2010.

[2] TJS. 5 useful (PHP) tricks and functions you may not know. http://www.tjs.co.uk, 2010.

[3] Checkmate - Play with problems. Optimize your PHP Code - Tips, Tricks and Techniques. http://abcphp.com, 2008.

[4] iBlog - Ilia Alshanetsky. PHP Optimization Tricks. http://ilia.ws, 2004.

from : http://www.think-techie.com/2010/04/php-web-development-tips-and-tricks.html

2 comments:

  1. Get most recent Web Development Ideas or strategies that can assist your organization with going advanced in 2019. Discover more about the latest tips that may assist you with working on your web ventures.

    ReplyDelete
  2. Nice post,

    Altorum Leren, a leading IT, Services Company, specializes in Software Application Development, Cloud Computing, UX/UI, Full Stack Development, IoT, Artificial Intelligence, Blockchain, DevOps, Front End Web Development, Back End Web Development, Mobile App Development and more. https://altorumleren.com/services/mobile-app-development/

    ReplyDelete