对C在C#虚拟呼叫速度++ [英] Speed of virtual call in C# vs C++

查看:121
本文介绍了对C在C#虚拟呼叫速度++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎记得读书的地方,在C#中的虚拟呼叫的成本不高,相对而言,在C ++。这是真的?如果是这样的 - 为什么

I seem to recall reading somewhere that the cost of a virtual call in C# is not as high, relatively speaking, as in C++. Is this true? If so - why?

推荐答案

C#虚拟呼叫必须检查这个被空和C ++虚拟呼叫没有? 。所以,我看不到一般为何C#虚拟呼叫会更快。在特殊情况下,C#编译器(或JIT编译器)的可能的能够内联虚拟呼叫优于C ++编译器,一个C#编译器获得更好的类型信息。调用方法指令的有时的是C ++慢一些,因为C#JIT可能能够使用更快的指令只对付一个小的偏移,因为它更多地了解运行时的内存布局和处理器型号,然后C ++编译器。

A C# virtual call has to check for "this" being null and a C++ virtual call does not. So I can’t see in generally why a C# virtual calls would be faster. In special cases the C# compiler (or JIT compiler) may be able to inline the virtual call better then a C++ compiler, as a C# compiler has access to better type information. The call method instruction may sometimes be slower in C++, as the C# JIT may be able to use a quicker instruction that only copes with a small offset as it know more about the runtime memory layout and processor model then a C++ compiler.

但是,我们正在谈论处理器指令一把顶多在这里。在调制解调器超标量处理器,它很可能是空检查诲是在同一时间的call方法运行,因此不花时间。

However we are talking about a handful of processor instruction at most here. On a modem superscalar processor, it is very possible that the "null check" instruct is run at the same time as the "call method" and therefore takes no time.

它也很可能所有的处理器指令将已经在是1级高速缓存,如果呼叫是使在一个循环。但数据不太可能是高速缓存,从主存储器读出一个数据值,这些天的成本是相同的,从1级高速缓存运行指令100秒。因此,这是不吉利的,在实际应用虚拟呼叫的成本是更多的则极少数地方甚至衡量的。

It is also very likely that all the processor instructions will already in be the level 1 cache if the call is make in a loop. But the data is less likely to be caches, the cost of reading a data value from main memory these days is the same as running 100s of instructions from the level 1 cache. Therefore it is unlucky that in real applications the cost of a virtual call is even measurable in more then a very few places.

,在C#代码使用几以上指令当然会减少代码可以容纳在高速缓存中的量的事实,这样的效果是无法预测。

The fact that the C# code uses a few more instructions will of course reduce the amount of code that can fit in the cache, the effect of this is impossible to predict.

(如果C ++类使用多个固有那么成本比较多,由于有修补的this指针。同样C#接口的加入重定向的另一个层面。)

(If the C++ class uses multiple inherence then the cost is more, due to having to patch up the "this" pointer. Likewise interfaces in C# add another level of redirection.)

这篇关于对C在C#虚拟呼叫速度++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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