Page 1 of 1

PHP try\catch blocks: Drawbacks?

Posted: Fri Feb 05, 2010 9:26 am
by jeff00seattle
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

Re: PHP try\catch blocks: Drawbacks?

Posted: Fri Feb 05, 2010 9:56 am
by JNettles
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?

Posted: Fri Feb 05, 2010 10:15 am
by JNettles
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. :wink: