不抛出异常时的性能(C ++) [英] Performance when exceptions are not thrown (C++)

查看:111
本文介绍了不抛出异常时的性能(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多关于C ++异常和我看到的东西,特别是异常表现是一个难题。我甚至试图在g ++的引擎盖下查看汇编中的异常情况。



我是一个C程序员,因为我喜欢低级语言。前段时间我决定使用C ++,因为它的成本很低,可以使我的生活变得更简单(对结构,模板等进行分类)。



回到我的问题,正如我看到例外仅在发生时生成开销,因为它需要很长的跳转和比较指令来找到适当的异常处理程序。在正常的程序执行(没有错误的情况下)异常开销等于正常的返回码检查。对我有用[0]丢个板砖[0]引用举报管理/ questions / 3589819 / overhead-of-exception-handling-in-d / 3590198#3590198> here 。



异常处理开销是平台特定的,取决于您正在运行的操作系统,编译器和CPU架构。



对于Visual Studio,Windows和x86,即使异常是没有抛出编译器生成附加代码以跟踪当前的范围,后者用于确定要调用的析构函数,以及开始搜索异常过滤器和处理程序的位置。范围更改由尝试块触发,并创建具有析构函数的对象。



对于Visual Studio,Windows和x86-64,当不抛出异常时,成本基本为零。 x86-64 ABI有一个比x86更加严格的关于异常处理的协议,而且操作系统做了很大的工作,所以程序本身不需要跟踪尽可能多的信息来处理异常。



当发生异常时,成本是显着的,这就是为什么它们只能在真正例外情况下发生。处理x86-64上的异常比x86更昂贵,因为架构针对更常见的例外情况进行了优化。


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 exceptions are represented in assembly.

I'm a C programmer, because I prefer low level languages. Some time ago I decided to use C++ over C because with small cost it can make my life much easier (classes over structures, templates etc.).

Returning back to my question, as I see exceptions do generate overhead bud only when they occur, because it require a long sequence of jumps and comparisons instructions to find a appropriate exception handler. In normal program execution (where is no error) exceptions overhead equals to normal return code checking. Am I right?

解决方案

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.

这篇关于不抛出异常时的性能(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆