php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40002 Try/Catch performs poorly
Submitted: 2007-01-02 19:47 UTC Modified: 2007-01-09 15:07 UTC
From: public at syranide dot com Assigned: dmitry (profile)
Status: Closed Package: Performance problem
PHP Version: 5.2.0 OS: Windows XP 32bit
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
18 + 38 = ?
Subscribe to this entry?

 
 [2007-01-02 19:47 UTC] public at syranide dot com
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

Add a Patch

Pull Requests

Add a Pull Request

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.
 [2007-01-09 15:07 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_2.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 20 09:03:33 2022 UTC