Php5 exception overhead

Filed under: Programming · Date: Tue Feb 28 23:25:10 2006

PHP introduced exceptions with try and catch primitives in version 5. Exceptions are found in other major Object-Oriented programming languages; adding them to PHP made it more usable for OO practices.

In PHP5 Power Programming the authors advices us not to use exceptions for flow control, because exceptions are much slower than returning values. This argument was not backed up by data, so I decided to do some tests to find if there was any overhead.

Test Setup

I wrote a simple script for testing exception overhead. The purpose of the script is to determine the cost of:

Using exceptions for error handling
Exceptions were invented as a device for handling errors in multiple levels of code. This test sets up a try/catch check around a code which doesn't throw exceptions (in our test).
Using exceptions for flow control
Exceptions are often abused for flow control. This test sets up a try/catch check on a code that throws an exception.
Returning false on error
The traditional procedural way of doing things is return false, or a similar value on errors. This way of coding is easy to achieve, but errors can be easily lost when function calls are nested; That's where exceptions help us.

The Test

Running my test script I got the following results:

Testing 100000 iterations of testInvoke: 1.80242395401
Testing 100000 iterations of testInvokeNested: 3.38034105301
Testing 100000 iterations of testNoInvoke: 0.362043142319
Testing 100000 iterations of testNoInvokeNested: 0.439126968384
Testing 100000 iterations of testReturnFalse: 0.375117063522
Testing 100000 iterations of testReturnFalseNested: 0.454220056534
Testing 100000 iterations of testReturnFalseStatic: 0.457010984421
Testing 100000 iterations of testReturnFalseObject: 0.66472196579
testNoInvoke mean time is 0.000004 s
testReturnFalse mean time is 0.000004 s
testNoInvokeNested mean time is 0.000004 s
testReturnFalseNested mean time is 0.000005 s
testReturnFalseStatic mean time is 0.000005 s
testReturnFalseObject mean time is 0.000007 s
testInvoke mean time is 0.000018 s
testInvokeNested mean time is 0.000034 s


Using exceptions for flow control is about six times as expensive as returning false or using exceptions for error handling.

As the above results show, error handling with exceptions does not add overhead to the execution of the code. When error-causing calls are nested, the code with try/catch blocks seem to be running slightly faster than the non-catching code. This was a surprise for me, since I had anticipated to find both "return false" tests at the top of the chart. I have no explanation for why this happens, but the results show there's no penalty for using exceptions for error handling.

The object tests are there to prove that the exception overhead is not caused entirely from creating new objects of the Exception class. The tests testReturnFalseStatic and testReturnFalseObject run 2-3 times faster than exception invoking code. Other than the object overhead tests, the exception tests do not use classes.


Comments are disabled for this post.