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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: public at syranide dot com
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC