PHP try\catch blocks: Drawbacks?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

PHP try\catch blocks: Drawbacks?

Post 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
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: PHP try\catch blocks: Drawbacks?

Post by JNettles »

Hit, yes. Performance issue - no. The price of additional cycles is worth the cost of not properly handling errors in your code.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: PHP try\catch blocks: Drawbacks?

Post 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:
Post Reply