有人可以解释一下 CLR/DLR 子类开销吗?

发布于 2024-10-10 11:05:55 字数 245 浏览 0 评论 0原文

如果我有一个使用 CLR 兼容代码(即在 C# 中)编写的库,并且我使用 DLR(即在 IronPython 中)对其中一些类进行子类化,那么与 if 相比,这对我的 CLR 库(即转换等)的速度有任何影响吗?它仅使用符合 CLR 的代码进行子类化?

我希望答案是“否”,只要 DLR 不覆盖我的 CLR 库所需的任何代码。我可以编写一些代码来根据经验对答案进行基准测试,但想知道你们中是否有人知道。

非常感谢,新年快乐!

约翰

If I have a library written using CLR compliant code (i.e. in C#) and I subclass some of those classes using the DLR (i.e. in IronPython) does this have any effect on the speed of my CLR library (i.e. casting etc) compared to if it were subclassed using only CLR compliant code?

I'm hoping the answer is 'no' as long as the DLR does not override any of the code required my my CLR library. I can write some code to empirically benchmark the answer but wondered if any of you knew.

Thank you very much and happy new year!

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓味的萝莉 2024-10-17 11:05:55

您的库代码中根本没有添加任何开销。引入新的子类根本不会改变 CLR 的行为。您可以想象一个 CLR 版本,它执行类层次结构分析或运行时优化,这些优化会内联方法或优化强制转换,其中其他子类的存在可能会默认这些优化。但我不知道有任何 CLR 实现可以进行这些优化(当然 MS CLR 不会)。

但使用 Python 子类可能会慢一些。这是因为 Python 子类将重写所有虚拟成员 - 这些重写随后需要分派到实现重载的 Python 函数或基类。因此,每当您使用 Python 中的子类化对象时,调用虚拟方法都会稍微慢一些。额外的开销将包括对 Python 类型对象进行一个或多个字典查找,以查看该方法是否在那里实现。

There's no overhead added in your library code at all. Introducing the new subclasses won't alter the behavior of the CLR at all. You could imagine a version of the CLR that does Class Hierarchy Analysis or runtime optimizations which inline methods or optimize away casts where the presence of additional subclasses could default those optimizations. But I don't know of any CLR implementations which do those optimizations (certainly the MS CLR does not).

But working with the Python subclasses could be a little slower. That's because the Python subclass will override all of the virtual members - those overrides will then need to dispatch to the Python function implementing the overload or to the base class. So whenever you're working with an object subclassed in Python it will be a little slower on calls to virtual methods. The extra overhead will consist of one or more dictionary lookups into the Python type object to see if the method is implemented there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文