Description:
------------
Try/Catch-statements in PHP is performing rather poorly, main problem that seems to be that the Catch-clauses are always tried regardless of an exception being thrown or not.
Although Try/Catch is pretty expensive (about twice of @), the worst part is that it scales linear with each Catch.
Of course that might be very hard to not do and is not a problem, but the problem arises from the linear scaling followed by Catch-clauses always impacting performance, regardless of an Exception being thrown or not.
Reproduce code:
---------------
$start = microtime(TRUE);
class PHPException extends Exception {}
for($i = 0; $i < 100000; $i++) {
try {}
catch(PHPException $e) {}
catch(PHPException $e) {}
catch(PHPException $e) {}
catch(PHPException $e) {}
catch(PHPException $e) {}
}
echo microtime(TRUE) - $start;
Expected result:
----------------
Note that the above code is rather strupid in nature, but the result is the same regardless.
From the above I would expect very similar performance to having only one Catch-clause (or to be more precise, that in the event of no exception being thrown performance is not linear to the number of Catch-clauses).
Actual result:
--------------
(nothing useful)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

[2007-01-02 19:58 UTC] public at syranide dot com

Should've mentioned this in the report, but one reason behind that it should not scale linear is because just as well as adding more clauses you could instead just make one catch-all-clause.
This clause internally matches exceptions (using is_a etc) and if none is matched then it is thrown again, which should have equal functionality, but more clauses does not affect performance if no exception is thrown.