For better programming practice ??
1) Optimize loop :-
.Eliminate repeated calls to the same function with the same parameters.
·Inline small, simple functions.
Eliminate repeated calls
------------------------
Why would we repeatedly call the same function with the same parameters?
Consider the following example :-
for ( $i=0; $i<count($myArray); ++$i )
// do something
call to count($myArray) is made once every iteration and the value returned by count($myArray) remains constant.
So you can write it more efficiently as follows:
$arraySize = count($myArray);
for ( $i=0; $i<$arraySize; ++$i )
// do something
Note: Notice how the call to count($myArray) is made just once now rather than once per iteration in the initial code example.
This second approach does create another, temporary variable, but it gives better overall performance.
also watch for code similar to the following:
$myConstString = "This is some constant text";
for ( $i=0; $i<$arraySize; ++$i )
{
print trim(strtolower($myConstString));
// do something
}
Provided the above loop does not modify $myConstString, then the call to trim(strtolower($myConstString)) will always return the same value
(in this instance, a string). You can move this function call to just before the loop, and store the return value in a temporary variable.
So we may watch for similar optimizations in our code.
******** END**************
Inline small, simple functions
------------------------------
Often functions are defined that contain just one or two lines of code in the body. In this case, the overhead associated with calling and
returning from the function may be large compared to the actual work done by the function. Ideally, the compiler would detect this and
replace the call to the function with the function body.
For example, consider the following code fragment:
function average( $a, $b )
{ return ($a + $b) / 2; }
...
for ( $i=0; $i<$arraySize; ++$i )
$av[$i] = average( $array1[$i], $array2[$i] );
And a more efficient implementation is;
for ( $i=0; $i<$arraySize; ++$i )
$av[$i] = ( $array1[$i] + $array2[$i] ) / 2;
Only use this optimization for small functions and in places where the function could be called thousands of times. For the larger funtions readability and
maintainability would efect.
Create and destroy objects within a loop only when necessary. Often, you can create an object outside of the loop. Then you can either treat it as read-only
within the loop or, if different object settings are required in each iteration, modify its properties instead of destroying one object and creating a new one.
For example:
for ( $i=0; $i<$arraySize; ++$i )
{
$pt = new Point( $i, $myArray[$i] );
// do something with $pt
}
Assume a Point object has two properties of interest: x and y.In each iteration of the loop, a new Point object is created (with the new keyword) and destroyed.
As an alternative, you can create one instance before the loop and then modify its properties in each iteration. The code would be as follows:
$pt = new Point();
for ( $i=0; $i<$arraySize; ++$i )
{
$pt->x = $i;
$pt->y = $myArray[$i];
// do something with $pt
}
2)Eliminating redundant code:-
When modifying existing code, be careful to remove any redundant code. For example, when I worked on code that several previous developers had modified, I found
assignments to a variable that were always executed, but the resulting variable was never used.
3)Avoiding comments that fail to add value :-
If you need a comment to explain the code, add information that will be helpful to other developers. If not, leave the comment out. It clutters up the code and
potentially adds a small overhead in any scripting language.
4)Reordering switch...case statements:-
When using a switch...case statement, try to place the most frequently-occurring cases at the top of the list. This improves speed and efficiency for the most
common cases.
5)Using multi-dimension arrays with caution:-
Try to avoid excessive use of arrays, especially arrays of arrays, unless you really are handling data with two or more dimensions.That is more efficient to use the
single dimension array with the place of multidimension.
Tweaking PHP code
-----------------
For better programming practice ??
1)Using pre-increment versus post-increment:-
According to the Zend Optimizer Technical FAQ, pre-incrementation is a faster operation than post-incrementation. This is one of the simpler optimizations performed
by the Zend Optimizer.Therefore, it is recommended that you use pre-incrementation rather than post-incrementation whenever possible.Similarly, you should use
pre-decrement instead of post-decrement whenever possible.
2)Quoting strings:-
strings within single quotes are not parsed, they are executed more quickly than strings inside double quotes that must be parsed first.
3)Understanding the difference between require and include:-
When you use the require keyword, the named file is read in, parsed, and compiled when the file using the require keyword is compiled. When a file containing the
include keyword is compiled, the named file is not read in, parsed, and compiled initially. Only when that line of code is executed is the file read, parsed and
compiled. Only use the require keyword if you know you will always need that named file in the current script. If you might use its functions, use include instead.
PHP opens up all files that are required, but only opens included files as needed.
4)Making effective use of built-in functions:-
PHP has a very rich set of built-in functions. In general, using an appropriate built-in function is more efficient than trying to implement the same functionality
in PHP manually.
An example of this is the date function. I have seen several instances where a developer has tried to output a date by first constructing an array with the getdate
function. Then, to generate the required output, the developer accesses the various elements of the array.
5)Reducing looping with built-in functions:-
Try to use the biut-in function, these are more efficient then creating own function with some loops.
- regards !
vikas dwivedi
.Eliminate repeated calls to the same function with the same parameters.
·Inline small, simple functions.
Eliminate repeated calls
------------------------
Why would we repeatedly call the same function with the same parameters?
Consider the following example :-
for ( $i=0; $i<count($myArray); ++$i )
// do something
call to count($myArray) is made once every iteration and the value returned by count($myArray) remains constant.
So you can write it more efficiently as follows:
$arraySize = count($myArray);
for ( $i=0; $i<$arraySize; ++$i )
// do something
Note: Notice how the call to count($myArray) is made just once now rather than once per iteration in the initial code example.
This second approach does create another, temporary variable, but it gives better overall performance.
also watch for code similar to the following:
$myConstString = "This is some constant text";
for ( $i=0; $i<$arraySize; ++$i )
{
print trim(strtolower($myConstString));
// do something
}
Provided the above loop does not modify $myConstString, then the call to trim(strtolower($myConstString)) will always return the same value
(in this instance, a string). You can move this function call to just before the loop, and store the return value in a temporary variable.
So we may watch for similar optimizations in our code.
******** END**************
Inline small, simple functions
------------------------------
Often functions are defined that contain just one or two lines of code in the body. In this case, the overhead associated with calling and
returning from the function may be large compared to the actual work done by the function. Ideally, the compiler would detect this and
replace the call to the function with the function body.
For example, consider the following code fragment:
function average( $a, $b )
{ return ($a + $b) / 2; }
...
for ( $i=0; $i<$arraySize; ++$i )
$av[$i] = average( $array1[$i], $array2[$i] );
And a more efficient implementation is;
for ( $i=0; $i<$arraySize; ++$i )
$av[$i] = ( $array1[$i] + $array2[$i] ) / 2;
Only use this optimization for small functions and in places where the function could be called thousands of times. For the larger funtions readability and
maintainability would efect.
Create and destroy objects within a loop only when necessary. Often, you can create an object outside of the loop. Then you can either treat it as read-only
within the loop or, if different object settings are required in each iteration, modify its properties instead of destroying one object and creating a new one.
For example:
for ( $i=0; $i<$arraySize; ++$i )
{
$pt = new Point( $i, $myArray[$i] );
// do something with $pt
}
Assume a Point object has two properties of interest: x and y.In each iteration of the loop, a new Point object is created (with the new keyword) and destroyed.
As an alternative, you can create one instance before the loop and then modify its properties in each iteration. The code would be as follows:
$pt = new Point();
for ( $i=0; $i<$arraySize; ++$i )
{
$pt->x = $i;
$pt->y = $myArray[$i];
// do something with $pt
}
2)Eliminating redundant code:-
When modifying existing code, be careful to remove any redundant code. For example, when I worked on code that several previous developers had modified, I found
assignments to a variable that were always executed, but the resulting variable was never used.
3)Avoiding comments that fail to add value :-
If you need a comment to explain the code, add information that will be helpful to other developers. If not, leave the comment out. It clutters up the code and
potentially adds a small overhead in any scripting language.
4)Reordering switch...case statements:-
When using a switch...case statement, try to place the most frequently-occurring cases at the top of the list. This improves speed and efficiency for the most
common cases.
5)Using multi-dimension arrays with caution:-
Try to avoid excessive use of arrays, especially arrays of arrays, unless you really are handling data with two or more dimensions.That is more efficient to use the
single dimension array with the place of multidimension.
Tweaking PHP code
-----------------
For better programming practice ??
1)Using pre-increment versus post-increment:-
According to the Zend Optimizer Technical FAQ, pre-incrementation is a faster operation than post-incrementation. This is one of the simpler optimizations performed
by the Zend Optimizer.Therefore, it is recommended that you use pre-incrementation rather than post-incrementation whenever possible.Similarly, you should use
pre-decrement instead of post-decrement whenever possible.
2)Quoting strings:-
strings within single quotes are not parsed, they are executed more quickly than strings inside double quotes that must be parsed first.
3)Understanding the difference between require and include:-
When you use the require keyword, the named file is read in, parsed, and compiled when the file using the require keyword is compiled. When a file containing the
include keyword is compiled, the named file is not read in, parsed, and compiled initially. Only when that line of code is executed is the file read, parsed and
compiled. Only use the require keyword if you know you will always need that named file in the current script. If you might use its functions, use include instead.
PHP opens up all files that are required, but only opens included files as needed.
4)Making effective use of built-in functions:-
PHP has a very rich set of built-in functions. In general, using an appropriate built-in function is more efficient than trying to implement the same functionality
in PHP manually.
An example of this is the date function. I have seen several instances where a developer has tried to output a date by first constructing an array with the getdate
function. Then, to generate the required output, the developer accesses the various elements of the array.
5)Reducing looping with built-in functions:-
Try to use the biut-in function, these are more efficient then creating own function with some loops.
- regards !
vikas dwivedi
Comments
Post a Comment