For example, if I implement some simple object caching, which method is faster?
1. return isset($cache[$cls]) ? $cache[$cls] : $cache[$cls] = new $cls;
2. return @$cache[$cls] ?: $cache[$cls] = new $cls;
I read somewhere @ takes significant time to execute (and I wonder why), especially when warnings/notices are actually being issued and suppressed. isset() on the other hand means an extra hash lookup. So which is better and why?
I do want to keep E_NOTICE on globally, both on dev and production servers.
I ran timing tests for both cases, using hash keys of various lengths, also using various hit/miss ratios for the hash table, plus with and without
E_NOTICE.The results were: with
error_reporting(E_ALL)theisset()variant was faster than the@by some 20-30%. Platform used: command line PHP 5.4.7 on OS X 10.8.However, with
error_reporting(E_ALL & ~E_NOTICE)the difference was within 1-2% for short hash keys, and up 10% for longer ones (16 chars).Note that the first variant executes 2 hash table lookups, whereas the variant with
@does only one lookup.Thus,
@is inferior in all scenarios and I wonder if there are any plans to optimize it.