What are some of the most expensive operations in PHP? I know things like overusing the @ operator can be expensive. What else would you consider?
Anything that's going though a network connection -- like calling a webservice, for instance : it'll generally take more time than doing an operation locally.
(Even if it doesn't cost much CPU, it'll cost time)
foreach()
statements, especially with nesting, are frequently expensive; though that's as much my naive -and occasionally poorly-planned- approach to programming as php's fault.
Though I think it's true, also, of JS and other languages, so almost certainly my fault. =/
"Hello $name"
syntax is slower than
'Hello ' . $name
also __get() __set() __call()
, etc are slow
and, if you care so much, you can use optimized structures from SPL
I'd say SQL queries inside loops. Such as this:
foreach ($db->query('SELECT * FROM categories') as $cat)
{
foreach ($db->query('SELECT * FROM items WHERE cat_id = ' . $cat['cat_id']) as $item)
{
}
}
Which, for the record, could be shortened into something like this:
$sql = 'SELECT c.*, i.*
FROM categoriess c
LEFT JOIN items i USING (cat_id)
ORDER BY c.cat_order';
foreach ($db->query($sql) as $row)
{
}
Rather than trying to figure out potential areas that are slow, use a profiling tool. Installing xDebug was probably one of the easiest and best things I've done to improve the code I write. Install with WinCacheGrind (or the correct version for your OS) for best results.
- serialize() is slow, as is eval(), create_function(), and spawning additional processes via system() and related functions.
- beware of anything APC can't cache -- conditional includes, eval()ed code, etc.
- Opening database connections. Always cache your connections and re-use them.
- Object cloning
- Regular expressions. Always use the normal string operations over a regular expression operation if you don't need the functionality of a regexp, e.g. use str_replace() over preg_replace() where possible.
- Logging and disk writes can be slow - eliminate unnecessary logging and file operations
Some micro-optimizations that are good practice, but won't make much difference to your bottom line performance:
- Using echo is faster than print
- Concatenating variables is faster than using them inline in a double-quoted string.
- Using echo with a list of arguments is faster than concatenating the arguments. Example:
echo 'How are you ',$name,' I am fine ',$var1
is faster thanecho 'How are you '.$name.' I am fine '.$var1
- Develop with Notices and Warnings turned on. Making sure they don't get triggered saves PHP from having to run error control on them.
From my own experience the most expensive operation in real terms is the echo
statement. Try and join all string together before outputting them to the browser, followed by database calls especially joins!
Code can also sometimes get a x10 performance increase by just simply refactoring your algorithms and data structures. Get any program and try to half its length, can you half it again?
curl_exec() is very slow, compared to typical operations. Also, most str_* operations are faster than regex operations.