C#动态关键字 - 运行时罚? [英] C# Dynamic Keyword — Run-time penalty?

查看:116
本文介绍了C#动态关键字 - 运行时罚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定是否一个实例在C#中的平均动态:


  1. 编译器不执行编译时类型检查,但运行时检查发生像它总是所有实例。


  2. 编译器不执行编译时类型检查,但运行时检查发生的,不像其他任何非动态的实例。


  3. 同2,这带有性能损失(平凡?可能显著?)。



解决方案

现在的问题是非常令人困惑。


  

确定是否一个实例在C#中的平均动态:


通过定义的实例你的意思是声明一个变量?


  

编译器不执行编译时类型检查,但运行时检查发生像它总是所有实例。


你说的运行时检查像它总是呢?你有什么想法运行时检查?你想通过 IL验证的执行检查的的,或者是你想造成的演员,或者是运行时类型检查的?

也许这将是最简单地解释什么是动态的呢。

首先,动态的是从的角度来看的编译的一个的键入的。从的角度来看的 CLR 的,有没有这样的事情的动态的;由code实际运行时间,动态的所有实例都换成了在生成的code对象。

,编译器将前pressions型类型的动态完全一样的前pressions的对象的的的,除了所有的操作上的前$的值p $ pssion进行了分析,编译和运行时执行的 的基础上,实例的运行时类型的。我们的目标是,执行code具有相同的语义的,如果编译器的早知道的运行时类型在编译时。

您的问题似乎是关于性能。

回答性能问题的最好办法是的尝试找出的 - 如果你需要实实在在的数字,你应该做的是写code两种方式,采用动态和使用已知类型,然后走出一个秒表和比较的时间安排。这是要知道的唯一途径。

不过,让我们考虑一些操作的性能影响在抽象的层面。假设你有:

  INT X = 123;
INT Y = 456;
INT Z = X + Y;

添加两个整数大约需要一秒钟的大多数硬件十亿分之一,这些天。

会发生什么,如果我们让它动态?

 动态X = 123;
动态Y = 456;
动态Z = X + Y;

现在,这是什么做在运行时?这盒123和456为对象,在堆上分配内存,并做一些拷贝。

然后,它开始了DLR,并要求DLR有这个code网站被同类型的X和Y是int和诠释编译一次了吗?

在此情况下,答案是否定的。在DLR然后启动C#编译器可以分析加前pression的一个特殊版本,执行重载决议,并吐出一个的前pression树的描述它增加了在一起的两个拉姆达整数。在DLR然后编译该拉姆达为动态生成IL,其中JIT编译器,然后即时编译器。在DLR然后缓存已编译状态,使的第二的时候,你要问,编译器并不需要做所有的工作从头再来。

这需要的的比纳秒。这需要纳秒的潜在成千上万。

这是否回答您的问题?我真的不明白你问在这里,但我正在做一个最好的猜测。

Does defining an instance as dynamic in C# mean:

  1. The compiler does not perform compile-time type checking, but run-time checking takes place like it always does for all instances.

  2. The compiler does not perform compile-time type checking, but run-time checking takes place, unlike with any other non-dynamic instances.

  3. Same as 2, and this comes with performance penalty (trivial? potentially significant?).

解决方案

The question is very confusing.

Does defining an instance as dynamic in C# mean:

By "defining an instance" do you mean "declaring a variable"?

The compiler does not perform compile-time type checking, but run-time checking takes place like it always does for all instances.

What do you mean by "run-time checking like it always does"? What run-time checking did you have in mind? Are you thinking of the checking performed by the IL verifier, or are you thinking of runtime type checks caused by casts, or what?

Perhaps it would be best to simply explain what "dynamic" does.

First off, dynamic is from the perspective of the compiler a type. From the perspective of the CLR, there is no such thing as dynamic; by the time the code actually runs, all instances of "dynamic" have been replaced with "object" in the generated code.

The compiler treats expressions of type dynamic exactly as expressions of type object, except that all operations on the value of that expression are analyzed, compiled and executed at runtime based on the runtime type of the instance. The goal is that the code executed has the same semantics as if the compiler had known the runtime types at compile time.

Your question seems to be about performance.

The best way to answer performance questions is to try it and find out - what you should do if you need hard numbers is to write the code both ways, using dynamic and using known types, and then get out a stopwatch and compare the timings. That's the only way to know.

However, let's consider the performance implications of some operations at an abstract level. Suppose you have:

int x = 123;
int y = 456;
int z = x + y;

Adding two integers takes about a billionth of a second on most hardware these days.

What happens if we make it dynamic?

dynamic x = 123;
dynamic y = 456;
dynamic z = x + y;

Now what does this do at runtime? This boxes 123 and 456 into objects, which allocates memory on the heap and does some copies.

Then it starts up the DLR and asks the DLR "has this code site been compiled once already with the types for x and y being int and int?"

The answer in this case is no. The DLR then starts up a special version of the C# compiler which analyzes the addition expression, performs overload resolution, and spits out an expression tree describing the lambda which adds together two ints. The DLR then compiles that lambda into dynamically generated IL, which the jit compiler then jits. The DLR then caches that compiled state so that the second time you ask, the compiler doesn't have to do all that work over again.

That takes longer than a nanosecond. It takes potentially many thousands of nanoseconds.

Does that answer your questions? I don't really understand what you're asking here but I'm making a best guess.

这篇关于C#动态关键字 - 运行时罚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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