#40002 [Asn->Csd]: Try/Catch performs poorly

From: Date: Tue, 09 Jan 2007 15:07:00 +0000
Subject: #40002 [Asn->Csd]: Try/Catch performs poorly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-107368@lists.php.net to get a copy of this message
ID: 40002 Updated by: dmitry@php.net Reported By: public at syranide dot com -Status: Assigned +Status: Closed Bug Type: Performance problem Operating System: Windows XP 32bit PHP Version: 5.2.0 Assigned To: dmitry New Comment: Fixed in CVS HEAD and PHP_5_2. Previous Comments: ------------------------------------------------------------------------ [2007-01-02 19:58:26] 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-02 19:47:15] 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) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40002&edit=1

« previous php.bugs (#107368) next »