当使用“动态”时,它对性能的影响程度? [英] How much it affects the performance when using "dynamic" ?

查看:110
本文介绍了当使用“动态”时,它对性能的影响程度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我只是理解动态与对象不同,可以从实例调用方法和变量,而不管它是哪种类型。

我想知道我是否可以使用动态关键字像往常一样,而不是每次都输入类型的名称,这也限制了只有一种类型实例的指针。



其次,我想知道它影响很多在我的代码中使用大量动态指针时的性能。



第三个问题,我可以突然将动态指针的指向实例更改为另一个实例(例如来自另一个动态指针)它会非常快速有效吗?



这里有一些代码:

 < span class =code-keyword> dynamic 实例; 
Instance = new Button();
Console.WriteLine(Instance.Text);
Instance = new Panel();
Console.WriteLine(Instance.Padding);
Instance = 10 ;
Console.WriteLine(Instance);



关于第3个问题:

 动态 A =  10 ; 
dynamic B = string;
B = A;
Console.WriteLine(A + B); // 预期输出:20





一般来说,我想知道使用动态会损害我的代码性能有多大。因为我正在研究真正关键的课程。但我还需要动态指针(如果不是那么对象指针更有问题)

解决方案

1)首先,试试吧!



2)如我所见,你混淆/混淆 var dynamic 。并且正在说指针。但是c#中没有指针。实际上有,但不是你的想法(参见: http://www.c-sharpcorner。 com / UploadFile / rajeshvs / PointersInCSharp11112005051624AM / PointersInCSharp.aspx [ ^ ])。所以,如果你真的想要使用这种C / C ++方法,继续,但这很难。



3)你对改变变量类型有什么期望?你什么也得不到。



4)在动态元数据类型的背后有一个逻辑,有很多代码。该代码需要时间来执行。这就是为什么在使用这种方式时你会失去相当大的性能。



5)动态在互操作情况下更有用:使用新的C#4动态关键字摆脱COM Interop DLL [ ^ ]



..等等:使用C#4.0动态冒险 - 在10分钟内展开ExpandoObject,ElasticObject和Twitter客户端 [ ^ ]

Hello , I just understood that dynamic is unlike the object and can call methods and variables from the instance and not matter which type it is .
I want to know if i can use the dynamic keyword as usual instead of typing the type''s name every time which also limiting the pointer for only one type of instances .

Secondly , i want to know much it affects the performance while using a lot of dynamic pointers in my code .

Third question , can i suddenly change the pointed instance of the dynamic pointer to another instance (for example from another dynamic pointer) and it will be quite fast and effective ?

Here some code :

dynamic Instance;
Instance = new Button();
Console.WriteLine(Instance.Text);
Instance = new Panel();
Console.WriteLine(Instance.Padding);
Instance = 10;
Console.WriteLine(Instance);


About the 3rd question :

dynamic A = 10;
dynamic B = "string";
B = A;
Console.WriteLine(A + B); // Expected output : 20



Generally i want to know how much using "dynamic" is hurts the performance of my code . Because i am working on really critical class . But i also need the dynamic pointers (if not then the object pointer which are more problematic)

解决方案

1) First of all, try it!

2) As I see, you mix/confuse var with dynamic. And are speaking pointers. But there are not pointers in c#. Actually there are, but not how you think (see: http://www.c-sharpcorner.com/UploadFile/rajeshvs/PointersInCSharp11112005051624AM/PointersInCSharp.aspx[^]). So if you really want to use this C/C++ approach, go on, but it will be hard.

3) What do you expect from changing the variable type? You gain nothing at all.

4) Behind the dynamic metatype there is a logic, there is lot of code. That code will take time to execute. This is why you will loose considerable performance when using it this way.

5) Dynamic is more useful in interop situations: Get rid of COM Interop DLL by using the new C# 4 dynamic keyword[^]

.. and more: Adventures with C# 4.0 dynamic - ExpandoObject, ElasticObject, and a Twitter client in 10 minutes[^]


这篇关于当使用“动态”时,它对性能的影响程度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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