views:

5941

answers:

7

What kind of performance implications are there to consider when using try-catch statements in php 5?

I've read some old and seemingly conflicting information on this subject on the web before. A lot of the framework I currently have to work with was created on php 4 and lacks many of the niceties of php 5. So, I don't have much experience myself in using try-catchs with php.

A: 

Generally speaking, they're expensive and not worthwhile in PHP.

Since it is a checked expressions language, you MUST catch anything that throws an exception.

When dealing with legacy code that doesn't throw, and new code that does, it only leads to confusion.

Good luck!

Ian P
For the record, PHP does not have "checked exceptions". All exceptions work like Java's "RuntimeException".
RJHunter
+4  A: 

I have not found anything on Try/Catch performance on Google but a simple test with a loop throwing error instead of a IF statement produce 329ms vs 6ms in a loop of 5000.

Daok
Mind posting the code you used so that it can be verified?
Unkwntech
+2  A: 

Generally, use an exception to guard against unexpected failures, and use error checking in your code against failures that are part of normal program state. To illustrate:

  1. Record not found in database - valid state, you should be checking the query results and messaging the user appropriately.

  2. SQL error when trying to fetch record - unexpected failure, the record may or may not be there, but you have a program error - this is good place for an exception - log error in error log, email the administrator the stack trace, and display a polite error message to the user advising him that something went wrong and you're working on it.

Exceptions are expensive, but unless you handle your whole program flow using them, any performance difference should not be human-noticeable.

Aeon
+16  A: 

One thing to consider is that the cost of a try block where no exception is thrown is a different question from the cost of actually throwing and catching an exception.

If exceptions are only thrown in failure cases, you almost certainly don't care about performance, since you won't fail very many times per execution of your program. If you're failing in a tight loop (a.k.a: banging your head against a brick wall), your application likely has worse problems than being slow. So don't worry about the cost of throwing an exception unless you're somehow forced to use them for regular control flow.

Someone posted an answer talking about profiling code which throws an exception. I've never tested it myself, but I confidently predict that this will show a much bigger performance hit than just going in and out of a try block without throwing anything.

Another thing to consider is that where you nest calls a lot of levels deep, it can even be faster to have a single try...catch right at the top than it is to check return values and propagate errors on every call.

In the opposite of that situation, where you find that you're wrapping every call in its own try...catch block, your code will be slower. And uglier.

Steve Jessop
+13  A: 

I was bored and profiled the following (I left the timing code out):

function no_except($a, $b) { 
    $a += $b;
    return $a;
}
function except($a, $b) { 
    try {
        $a += $b;
    } catch (Exception $e) {}
    return $a;
}

using two different loops:

echo 'no except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    no_except(5, 7);
}
echo 'no except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    try {
        no_except(5, 7);
    } catch (Exception $e) {}
}
echo 'except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    except(5, 7);
}
echo 'except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    try {
        except(5, 7);
    } catch (Exception $e) {}
}

With 1000000 runs on my WinXP box run apache and PHP 5.2.6:

no except with no surrounding try = 3.3296
no except with surrounding try = 3.4246
except with no surrounding try = 3.2548
except with surrounding try = 3.2913

These results were consistent and remained in similar proportion no matter which order the tests ran.

Conclusion: Adding code to handle rare exceptions is no slower than code the ignores exceptions.

jmucchiello
A: 

Thats a very good question!

I have tested it many times and never saw any performance issue ;-) It was true 10 years ago in C++ but I think today they have improved it a lot since its so usefull and cleaner.

But I am still afraid to surround my first entry point with it:

try {Controller::run();}catch(...)

I didnt test with plenty of functions call and big include .... Is anyone has fully test it already?

Tom
A: 

someone extremely smart at work told me try catches which don't throw will affect performance on a site with millions of users. based on the unit test posted showing equal performance, I'm wondering if this is related to an os level and/or web server specific situation. For instance, web server's implementation of asynchronous work occurs on child processes instead of threads.

Anyone know?

devadvocate