2 ms·
C++ exceptions, in modern implementations, should have no time overhead when no exception is thrown. C error codes have the overhead of comparisons to check if
by kentonv 10y ago
C++ exceptions, in modern implementations, should have no time overhead when no exception is thrown.
C error codes have the overhead of comparisons to check if the return value is an error return.
EDIT: To expand on this: Modern implementations of C++ exceptions build tables mapping code addresses to information about the exception context at that address. The table is created by the compiler and stored in a separate segment of the executable. If no exception is thrown, the table is never consulted. When an exception is thrown, the exception-handling code consults the tables while unwinding the stack. For each return address, the EH tables tell it what destructors need to be run to unwind that stack frame. So, there's no need for comparisons or branching in the code itself. Of course, table-driven execution is expected to be slow (for the same reason interpreted languages are slow), but the idea is to optimize for the common case where there are no exceptions.