Performance when exceptions are not thrown (C++)

后端 未结 2 1212
花落未央
花落未央 2021-01-04 09:40

I have already read a lot about C++ exceptions and what i see, that especially exceptions performance is a hard topic. I even tried to look under the g++\'s hood to see how

2条回答
  •  时光说笑
    2021-01-04 09:58

    Please see my detailed response to a similar question here.

    Exception handling overhead is platform specific and depends on the OS, the compiler, and the CPU architecture you're running on.

    For Visual Studio, Windows, and x86, there is a cost even when exceptions are not thrown. The compiler generates additional code to keep track of the current "scope" which is later used to determine what destructors to call and where to start searching for exception filters and handlers. Scope changes are triggered by try blocks and the creation of objects with destructors.

    For Visual Studio, Windows, and x86-64, the cost is essentially zero when exceptions are not thrown. The x86-64 ABI has a much stricter protocol around exception handling than x86, and the OS does a lot of heavy lifting, so the program itself does not need to keep track of as much information in order to handle exceptions.

    When exceptions occur, the cost is significant, which is why they should only happen in truly exceptional cases. Handling exceptions on x86-64 is more expensive than on x86, because the architecture is optimized for the more common case of exceptions not happening.

提交回复
热议问题