- calling an object method is faster then traping it with with "__call"
- calling a "static" method is faster then an object method
- calling a function is faster then calling a static method
- accessing a local variable is faster then then a global variable
- accessing a global variable is faster then an object property
- accessing an object property is faster then trapping it with "__get" and "__set"
- accessing an initialized variable is faster then accessing an uninitialized variable
- absolute paths in "include" and "require" are faster then relative
- merging several scripts in one file and then including it is faster then several includes
- "switch" is faster then "if ... else if ..." in some cases
- do not use regexs for simple string processing tasks
- avoid @ (error control operator)
- avoid notices and warnings in your scripts
- avoid unused variables and unused method parameters
- adding method parameter increases calling time
- adding method parameter type hint increases calling time
- unset variables that contain large amount of data or circular references
- $_SERVER['REQUEST_TIME'] contains script startup time
- cache page output or result of resource-consuming functions
- "echo" is faster than "print"
- "echo" accepts several arguments, you can use it instead of string concatenation
- "ob_start()" and "ob_end_clean()" is may be better then several string concatenations
- strings in single quotes ('...') are processed faster then strings in double quotes ("...")
- pre-increment (++$i) is faster then post-increment ($i++)
- "isset" is a faster alternative to "array_key_exists"
- an array is a faster alternative to a class with several fields
- "foreach" is better then "for" in many cases
- open resources (files, database connections, sockets) right before using them, free resources as soon as you do not need them
- do not fetch database fields which you do not use in your script
- use prepared database statements
- combine several queries in one when database supports it
Tuesday, March 10, 2009
PHP micro-optimization tips
Tuesday, December 23, 2008
Data structures in PHP 5.3
In the programming world there are quite a few well understood and explored data structures. Which are commonly used in tons of applications, still the only things PHP offered till 5.3 in regards to structuring data were arrays (more precise: hash tables) and objects. So people had to either abuse them for other structures or implement the structures themselves on top of these. Thanks to Etienne things now are changing and PHP's Standard PHP Library (SPL) extension will offer quite a few standard implementations of data structures. The implemented data structures are implemented using these classes:
* SplDoublyLinkedList
* SplStack
* SplQueue (SplPriorityQueue)
* SplHeap (SplMinHeap, SplMaxHeap)
* SplFixedArray
For more details head over at http://schlueters.de/blog/archives/90-Data-structures-in-PHP-5.3.html
* SplDoublyLinkedList
* SplStack
* SplQueue (SplPriorityQueue)
* SplHeap (SplMinHeap, SplMaxHeap)
* SplFixedArray
For more details head over at http://schlueters.de/blog/archives/90-Data-structures-in-PHP-5.3.html
Wednesday, December 17, 2008
Usage of IF condition in mysql
Scenario:
Suppose we have the field in the table lets say status which has the ENUM value ACTIVE,INACTIVE.Usually after getting the value of this field(through the query)
we used to write return ($result['status']=='ACTIVE')?1:0 in class methods.
Here we are getting the db result array and based on the check(ACTIVE/INACTIVE) we are returning the boolean value.Instead of that we can have that check in the query itself like below
SELECT IF(status='ACTIVE',1,0) AS statuscheck from table1
Note that in the condition we need to give single equal symbol
so the code return ($result['status']=='ACTIVE')?1:0 will be replaced by return $result['statuscheck'].
Suppose we have the field in the table lets say status which has the ENUM value ACTIVE,INACTIVE.Usually after getting the value of this field(through the query)
we used to write return ($result['status']=='ACTIVE')?
Here we are getting the db result array and based on the check(ACTIVE/INACTIVE) we are returning the boolean value.Instead of that we can have that check in the query itself like below
SELECT IF(status='ACTIVE',1,0) AS statuscheck from table1
Note that in the condition we need to give single equal symbol
so the code return ($result['status']=='ACTIVE')?
Tuesday, December 16, 2008
Google Turns its Back on Firefox
About a month ago, Google dropped StarOffice from the Google Pack, a downloadable package of free Internet and productivity software for Windows users. StarOffice is a desktop office suite based on the open source OpenOffice.org program and distribute by Sun, and we theorized that Google was laying the ground work for a future push to get users to dump their desktop software in favor of Google’s own web-based application offerings.
For more details head over at http://www.sitepoint.com/blogs/2008/12/15/google-turns-its-back-on-firefox/
For more details head over at http://www.sitepoint.com/blogs/2008/12/15/google-turns-its-back-on-firefox/
Friday, December 12, 2008
CodeIgniter - Open source PHP web application framework
For more details check it in http://roshanbh.com.np/2008/12/codeigniter-programmer-php.html
Monday, December 1, 2008
Introduction To Xajax
The Xajax library provides a useful and simple method of generating asynchronous requests with next to not knowledge of javascript. All the javascript code is generated internally and leaves the developer to simply implement PHP functions. This tutorials is an absolute basic beginners guide to getting started with xajax. Full source code is provided with an explaination of how the process works.
More details on this, check it on http://www.phpro.org/tutorials/Introduction-To-Xajax.html
More details on this, check it on http://www.phpro.org/tutorials/Introduction-To-Xajax.html
Friday, November 28, 2008
Quick and Easy Graphing with the Google Chart API
Google has created an API for displaying the data in terms of chart which will be helpful in visualizing the data for reporting.For more details head over at http://www.sitepoint.com/blogs/2008/11/28/quick-and-easy-graphing-with-the-google-chart-api/
Tuesday, November 25, 2008
Application Configuration
PHP applications come in many shapes and sizes. Some used locally from command line, and more commonly, for web based applications. More often than not, regardless of size or type, some form of configuration variables will be stored for global access. Usually these configuration options are held in one of these four types of containers.
* ini file
* XML file
* PHP file
* Database
Each options has its pros and cons. Here each of these options is explored to see which method is right for your application.
For more details head over at http://www.phpro.org/articles/Application-Configuration.html
* ini file
* XML file
* PHP file
* Database
Each options has its pros and cons. Here each of these options is explored to see which method is right for your application.
For more details head over at http://www.phpro.org/articles/Application-Configuration.html
Tuesday, November 18, 2008
GROUP_CONCAT in MySQL
I had the scenario for the table like below.
Table: test
id title
_________________
1 T2
1 T3
1 T1
2 L2
2 L1
I want the results like
id title
_________________
1 T2,T3,T1
2 L2,L1
For this, SQL would be
select id,GROUP_CONCAT('title') from test GROUP BY id ORDER by id
Now i want the values of title to be sorted.
This can be done using the below query
select id,GROUP_CONCAT('title' ORDER BY title) from test GROUP BY id ORDER by id
The results would be
id title
_________________
1 T1,T2,T3
2 L1,L2
Table: test
id title
_________________
1 T2
1 T3
1 T1
2 L2
2 L1
I want the results like
id title
_________________
1 T2,T3,T1
2 L2,L1
For this, SQL would be
select id,GROUP_CONCAT('title') from test GROUP BY id ORDER by id
Now i want the values of title to be sorted.
This can be done using the below query
select id,GROUP_CONCAT('title' ORDER BY title) from test GROUP BY id ORDER by id
The results would be
id title
_________________
1 T1,T2,T3
2 L1,L2
Tuesday, October 21, 2008
Some useful Linux commands
Zipping the contents
tar -cvzf Destination folder Source folder from where files need to copy
Extracting the contents
tar -xvzf Source folder to extract
Zip the contents excluding the folders
tar -cvzf Destination folder Source folder from where files need to copy
--exclude="contents" --exclude="tmp"
To delete all occurance of files start with some extension
Eg:
find . -name ".svn" -exec rm -rf {} \;
tar -cvzf Destination folder Source folder from where files need to copy
Extracting the contents
tar -xvzf Source folder to extract
Zip the contents excluding the folders
tar -cvzf Destination folder Source folder from where files need to copy
--exclude="contents" --exclude="tmp"
To delete all occurance of files start with some extension
Eg:
find . -name ".svn" -exec rm -rf {} \;
Subscribe to:
Posts (Atom)