Hi
In other languages, using try\catch block adds a performance hit because the compiler does not optimize the code within the block in order to more eazily catch errors.
So, is this also the case in PHP, is there a performance (speed and\or memory) hit in using try\catch blocks?
Thanks
Jeff in Seattle
PHP try\catch blocks: Drawbacks?
Moderator: General Moderators
-
jeff00seattle
- Forum Commoner
- Posts: 66
- Joined: Sat Feb 28, 2009 3:27 pm
Re: PHP try\catch blocks: Drawbacks?
Hit, yes. Performance issue - no. The price of additional cycles is worth the cost of not properly handling errors in your code.
Re: PHP try\catch blocks: Drawbacks?
I ran a quick speed test (nothing too scientific) - counting to from 0 to 10 million with a while loop.
Without try/catch -> Execution in 2.08517289162 seconds.
With try/catch (outside the loop)-> Execution in 2.08804297447 seconds
With try/catch (inside the loop) -> Execution in 2.46122717857 seconds.
There is a small hit, but definitely not crippling. If only we lived in a perfect world where there were no errors to catch.
Without try/catch -> Execution in 2.08517289162 seconds.
With try/catch (outside the loop)-> Execution in 2.08804297447 seconds
With try/catch (inside the loop) -> Execution in 2.46122717857 seconds.
There is a small hit, but definitely not crippling. If only we lived in a perfect world where there were no errors to catch.