例外 [英] Exceptions

查看:93
本文介绍了例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么C ++中的异常比OCaml中的异常慢6倍?


-

Jon D Harrop博士,Flying Frog Consultancy

科学家的客观CAML
http://www.ffconsultancy.com/product...ex.html?usenet

推荐答案

* Jon Harrop:
* Jon Harrop:

为什么C ++中的异常比OCaml中的异常慢6倍?
Why are exceptions in C++ ~6x slower than exceptions in OCaml?



语言不快或慢;实现是。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?

Languages are not fast or slow; implementations are.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




Jon Harrop写道:

Jon Harrop wrote:

为什么C ++中的异常比异常中的异常慢6倍OCaml的?
Why are exceptions in C++ ~6x slower than exceptions in OCaml?



请记住,C ++异常通常以这样的方式实现

他们可以透明地浏览不知道的帧。 />
关于异常的任何内容,包括由代码设置的框架,这些代码甚至不一定是用b ++编写的。它可以用C编写并用C编译器编译成b $ b,甚至可以用汇编语言手工编写---

长按照指令集架构''有关

的约定与堆栈有关。有任何析构函数运行的C ++代码,

并且没有捕获任何异常也可以编译,好像

异常不存在,如果事情是以这种方式实现。但是这个


是否可以减慢不关心异常的代码

,比如15%,以便将异常加速六倍?很多C ++的用户都不希望这种折衷。例外情况往往是在特殊情况下发生的罕见事件。


这是一个考虑因素:OCaml异常是否具有非ABI -OCaml

在同一图像中运行的组件可以用于互操作性吗?


最后,你比较了哪些编译器? OCaml有多少编译器

,在

例外区域,他们的作者认为重要的是什么类型的东西?

Remember that C++ exceptions are typically implemented in such a way
that they can transparently travel through frames that don''t know
anything about exceptions, including frames set up by code that wasn''t
necessarily even written in C++. It could be written in C and compiled
with a C compiler, or even written by hand in assembly language---as
long at follow the instruction set architecture''s conventions with
regard to the stack. C++ code that does have any destructors to run,
and doesn''t catch any exceptions can be also compiled as though
exceptions didn''t exist, if things are implemented this way. But this
means that the exception handling search has to parse its way through
the machine''s stack layout.

Would it be okay to slow down code that doesn''t care about exceptions
by, say, 15% in order to speed up exceptions six fold? A lot of the
users of C++ would not want that trade off. Exceptions tend to be
regarded as rare events that occur in, well, exceptional circumstances.

Here is a consideration: Do OCaml exceptions have an ABI that non-OCaml
components running in the same image can use for interoperability?

Lastly, what compilers have you compared, anyway? How many compilers
are there for OCaml, and what kinds of things, in the area of
exceptions, did their writers consider important?


Kaz Kylheku写道:
Kaz Kylheku wrote:

是否可以减慢不关心异常的代码比如说15%的b $ b,以便将异常加速六倍?很多C ++的用户都不希望这种折衷。例外情况往往被视为罕见的事件,这些事件发生在特殊情况下。
Would it be okay to slow down code that doesn''t care about exceptions
by, say, 15% in order to speed up exceptions six fold? A lot of the
users of C++ would not want that trade off. Exceptions tend to be
regarded as rare events that occur in, well, exceptional circumstances.



只有它们很慢。在OCaml中,异常是如此之快以至于广泛使用它们是非常好的。

Only if they are slow. In OCaml, exceptions are so fast that they are used
extensively.


这是一个考虑因素:OCaml异常是否具有非ABI -OCaml

在同一图像中运行的组件可以用于互操作性吗?
Here is a consideration: Do OCaml exceptions have an ABI that non-OCaml
components running in the same image can use for interoperability?



OCaml可以使用OCaml库中的各种宏来连接到C,以及
处理值和异常。我不知道如何实现例外情况

实现。


大概垃圾收集会使异常更快,因为C ++会有$

调用它所展开的每个范围内的所有析构函数。事实上,C ++

可能必须在每个堆栈框架中存储这样的信息,这可能会解释为什么它有如此大的堆栈框架......

OCaml can interface to C using a variety of macros from OCaml''s library to
handle values and exceptions. I don''t know how the exceptions are
implemented though.

Presumably garbage collection makes exceptions much faster, as C++ will have
to call all destructors in every scope that it unwinds past. In fact, C++
probably has to store such information in every stack frame, which might
explain why it has such big stack frames...


最后,你比较了哪些编译器? OCaml有多少编译器

,在

例外区域,他们的编写者认为重要的是什么类型的东西?
Lastly, what compilers have you compared, anyway? How many compilers
are there for OCaml, and what kinds of things, in the area of
exceptions, did their writers consider important?



只需一个OCaml编译器和g ++ 4.我的主要测试是平衡二叉树

设置实现。我插入一个

已经存在的元素时尝试使用异常保释,以便不加改变地返回原始树。在OCaml,

这比手动展开堆栈

更有效,更清晰。在C ++中,它要慢得多。


-

Jon D Harrop博士,飞蛙咨询公司

科学家的客观CAML
http://www.ffconsultancy.com/ product ... ex.html?usenet


这篇关于例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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