Wednesday, March 21, 2007
12 PHP optimization tips
1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
2. Avoid magic like __get, __set, __autoload
3. require_once() is expensive
4. Use full paths in includes and requires, less time spent on resolving the OS paths.
5. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
6. See if you can use strncasecmp, strpbrk and stripos instead of regex
7. preg_replace is faster than str_replace, but strtr is faster than preg_replace by a factor of 4
8. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
9. Error suppression with @ is very slow.
10. $row[’id’] is 7 times faster than $row[id]
11. Error messages are expensive
12. Do not use functions inside of for loop, such as for ($x=0; $x <count($array); $x) The count() function gets called each time.
References:-
http://www.moskalyuk.com/blog/php-optimization-tips/1272
Tuesday, March 20, 2007
Arrays as Multipurpose Data Structures
Although that JavaScript may seem limited on the data structure front at first glance, its Array class is much more versatile than the usual array type found in other programming languages (like C++ or Java). It's commonly used as an array or associative array, and this tip demonstrates how to use it as a stack, queue. Re-using the Array class instead of writing such data structures provides two benefits: First, time isn't wasted rewriting existing functionality, and second, the built-in browser implementation will be more efficient than its JavaScript counterpart.
Stack
A stack follows the Last-In First-Out (LIFO) paradigm: an item added last will be removed first. The Array class has 2 methods that provide stack functionality. they are push() and pop(). push() appends an item to the end of the array, and pop() removes and returns the last item in the array. The next code block demonstrates how to utilize each of them:
Eg:
var stack = [];
stack.push(2); // stack is now [2]
stack.push(5); // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i); // displays 5
Queue
A queue follows the First-In First-Out (FIFO) paradigm: the first item added will be the first item removed. An array can be turned into a queue by using the push() and shift() methods. push() inserts the passed argument at the end of the array, and shift() removes and returns the first item. let's see how to use them:
Eg:
var queue = [];
queue.push(2); // queue is now [2]
queue.push(5); // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i); // displays 2
Stack
A stack follows the Last-In First-Out (LIFO) paradigm: an item added last will be removed first. The Array class has 2 methods that provide stack functionality. they are push() and pop(). push() appends an item to the end of the array, and pop() removes and returns the last item in the array. The next code block demonstrates how to utilize each of them:
Eg:
var stack = [];
stack.push(2); // stack is now [2]
stack.push(5); // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i); // displays 5
Queue
A queue follows the First-In First-Out (FIFO) paradigm: the first item added will be the first item removed. An array can be turned into a queue by using the push() and shift() methods. push() inserts the passed argument at the end of the array, and shift() removes and returns the first item. let's see how to use them:
Eg:
var queue = [];
queue.push(2); // queue is now [2]
queue.push(5); // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i); // displays 2
When you can't find a calculator...
If you're using a JavaScript enabled version of Netscape Navigator, you can use it instead of your calculator to perform even very complex calculations. You don't have to go to a web site with an online calculator to do this. All you have to do is:
http://www.chami.com/tips/javascript/
- Type javascript:eval(
) in the url and press ENTER
For example, to add 1 and 3 type:
javascript:eval( 1 + 3 )
and press ENTER
http://www.chami.com/tips/javascript/
Javascript Tips
Detecting Browser and Browser Version:
eg:
For internet explorer:
navigator.userAgent.indexOf("msie")!= -1
navigator.appName
navigator.appVersion
Detecting Plugins:
navigator.mimeTypes
eg:navigator.mimeTypes['application/x-shockwave-flash'];
navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin
navigator.plugins["Shockwave Flash"];
To fetch the selected text:
mozilla/opera: window.getSelection().toString()
ie : document.selection.createRange().text
window.onerror Event:
onerror event fires whenever an JavaScript error occurs.
The onerror event is of the window object, a rather unusual object to be attached to. It is attached this way so it can monitor all JavaScript errors on a page, even those in the section of the page.
eg:
For internet explorer:
navigator.userAgent.indexOf("msie")!= -1
navigator.appName
navigator.appVersion
Detecting Plugins:
navigator.mimeTypes
eg:navigator.mimeTypes['application/x-shockwave-flash'];
navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin
navigator.plugins["Shockwave Flash"];
To fetch the selected text:
mozilla/opera: window.getSelection().toString()
ie : document.selection.createRange().text
window.onerror Event:
onerror event fires whenever an JavaScript error occurs.
The onerror event is of the window object, a rather unusual object to be attached to. It is attached this way so it can monitor all JavaScript errors on a page, even those in the section of the page.
Subscribe to:
Posts (Atom)