阅读开始 F# - Robert Pickering 我专注于以下段落:
Programmers coming from an OCaml background should be careful when using exceptions in F#. Because of the architecture of the CLR, throwing an exception is pretty expensive—quite a bit more expensive than in OCaml. If you throw a lot of exceptions, profile your code carefully to decide whether the performance costs are worth it. If the costs are too high, revise the code appropriately.
为什么,由于 CLR,如果
F#
抛出异常会更昂贵比 OCaml
?在这种情况下,适当修改代码的最佳方法是什么?
最佳答案
CLR 中的异常非常丰富,并且提供了很多细节。 Rico Mariani 在 the cost of exceptions 上发布了一个(旧的,但仍然相关的)帖子。在 CLR 中详细介绍了其中的一些内容。
因此,在 CLR 中引发异常比在其他一些环境(包括 OCaml)中具有更高的相对成本。
And what is the best way to revise the code appropriately in this case?
如果您希望在正常、非异常情况下引发异常,您可以重新考虑您的算法和 API,以完全避免异常。例如,尝试提供一个替代 API,您可以在其中测试环境,然后再引发异常。
关于exception - CLR 与 OCaml 异常开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964851/