Dec
11
2008
Tips for Optimising your PHP code came about after I was asked to check over a Web Application, a Junior developer created. My main job was to improve performance. After I looked over the code, I thought it would be a good idea to list some performance advice for any young developers learning PHP.
- If a method can be static, declare it static. Speed improvement is by a factor of 4.
- echo is faster than print
- Use echo’s mulitple parameters instead if string concatenation
- Set the maxvalue for your for-loops before and not in the loop
- Unset your variables to free memory, especially large array
- Avoid magic like __get(), __set() and __autoload()
- require_once() is expensive
- Use full paths in includes() and requires() as less time is spent on resolving the OS paths
- If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
- See if you can use strncasecmp(), strpbrk() and stripos() instead of regex()
- str_replace() is faster than preg_replace(), but strtr() is faster than str_replace() by a factor of 4
- If functions such as string replacement functions, accept 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.
- Its better to use select statements then multi if, else if statements
- Error suppression with @ is very slow
- Turn on Apaches mod_deflate
- Close your database connections when you’re done with them
- Use mysql_free_result() after you have got your record-set from a query
- $row['id'] is 7 times faster than $row[id]
- Error messages are expensive
- Do not use functions inside for-loops such as for($x = 0; $x < count($array); ++x$). The count() function gets called each time
- Incrementing a local variable in a method is fastest. Nearly the same as calling a local variable in a function
- Incrementing an global variable is 2 times slower than a defined local variable
- Incrementing an object property such as $this->total++ is 3 times slower than a local variable
- Incrementing an undefined local variable is 9-10 times slower than a pre-initialised one
- Just declaring a global variable without using it in a function also slows things down
- Method invocation appears to be independent of the number of methods defined in the class. I carried out a test on this adding a test method to a class and found that when I add 10 or more methods to the class before and after the test method there was no change in performance
- Methods in derived classes run faster than ones defined in the base class
- A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localVars++ operations
- Surrounding your string by ‘ instead of ” will make things interpret a little faster since PHP looks for variables inside “…” but not instead ‘…’ Of course you can only do this when you done need to have variables in a string!
- When echoing strings its faster to separate them by commas instead of dots
- PHP is served at least 2-10 times slower than static HTML by Apache so try use more static HTML pages and fewer scripts!
- PHP is compiled every time unless your PHP is cached. Install a PHP caching agent to increase performance by 25-100% by removing compile times
- Cache as much as possible. Use memcache which is a high performance memory onbject caching system intended to speed up dynamic web applications by alleviating database load
- 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 strlen() function. This function is pretty quick since its operation does not perform any calculation but merely return the already known length of the string available in the zval structure. (zval is an internal C struct used to store variables in PHP). However since the strlen() function is still somewhat slow the function call requires several operations such as lowercase and hashtable lookups followed by the execution of said function and in some instances you can improve the speed by using the isset() trick
- isset() is a language contruct and not a function meaning that its execution does not require function lookups and lowercase, meaning you have no overhead on top of your code
- When incrementing or decrementing the value of the variable $i++ happens to be a tad slower than ++$i. This is something I brought up on Twitter a few days ago. This is something PHP specific and does not apply to other languages so dont go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3
- Not everything has to be OOPHP. Often OO is much overhead, each method, each class or object call can consume a lot of memory
- Do not implement every data structure as a class, arrays are friendly and very useful!
- Dont split methods too much - think which code you will really reuse?
- Profile your code. Profiling shows you bottlenecks! The Xdebug debugger already contains a profiler
- mod_gzip which is available as Apache module compresses your data on the fly and can reduce the data transfer by up to 80%
no comments | tags: Optimising, PHP, Programming | posted in All Categories, Tools
{ Scroll back to the top of this page }
Dec
10
2008
Being an Irish man and all, it wouldn’t be right to have swearing in foreign languages on my blog without have a page dedicated to swearing in my own native tongue, Irish, or as some people like to call it, Gaelic.
So here is a list of insults and swear words in Irish!! Enjoy.. Tiocfaidh ar lá
- Dick - Bod
- Pussy - Báltaí
- Tit - Cíoch
- Shit - Cac
- Whore - Striapach
- Bitch - Bitseach
- Scumbag - Cac ar oineach
- Kiss my ass - Pói mo thóin
- Damn you - Damnú ort
- Fuck Off - Focáil leat
- Shut up - Bi ‘do thost / Dun do bhail
- Go fuck yourself - Téigh trasna ort féin
- Suck my dick - Díul mó bhad
- Your crazy - Tú tú glan as do mheabhair
- I’m cuming - Táim ag teacht
- I couldn’t care less / I don’t give a damn - Is cuma liom sa diabhal
- Shit / Damn it! - Damnú air!
no comments | tags: Gaelic, Irish, Languages, Swearing | posted in All Categories, Blogging, Daily Life
{ Scroll back to the top of this page }