编译器如何在C#中工作 [英] How does compiler works in C#

查看:102
本文介绍了编译器如何在C#中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些代码在编译器创建.exe的速度方面是否存在差异?



is there a differents between these codes on speed on how the compiler create the .exe ?

String s = Object1.Object2.GetCustomerName();// the Name can also be a Property
CallMethod(s);











and

CallMethod(Object1.Object2.GetcustomerName());





第二个如果在整个项目中完成,那么只需要更少的代码。如果会有更大的这样的东西,那么就很难阅读代码。



第一个在调试时非常有用,因为你可以看到非常快的价值你打电话给方法。代码会更清晰。



我说错了吗?



谢谢,



The second would just have less code if this is done in the whole project. If there would be bigger things like this it would just be hard to read the code.

The first would be very helpfull at debugging because you can see very fast the value you call the method. And the code would be much cleaner.

Am i saying something wrong?

Thanks,

推荐答案

在实践中?不,不是在发布版本中 - 中间变量将被优化。

在调试版本中可能存在差异,因为调试版本通常不使用积极的优化,如果它使用任何根本。



您可以自行检查:这解释了如何查看IL http://www.ginktage.com/2011/04/display-il-code-in-visual-studio-2010/ [<一个href =http://www.ginktage.com/2011/04/display-il-code-in-visual-studio-2010/target =_ blanktitle =New Window> ^ ]





但是......说过大部分优化是在JIT编译器上完成的,它使用IL作为输入,如果你正在调试,你真的不在乎...... http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx [ ^ ]



这很复杂:我个人会使用第一个版本,因为它更容易阅读和调试 - 但在实践中它完全没有任何区别! :笑:
In practice? No, not in release versions - the intermediate variable will be optimised out.
In debug versions there is likely to be a difference as debug versions generally do not employ as aggressive optimisations, if it uses any at all.

You can check for yourself: this explains how to view the IL http://www.ginktage.com/2011/04/display-il-code-in-visual-studio-2010/[^]


But...having said that most of the optimisation is done at the JIT compiler which uses the IL as the input, and doesn't really care if you are debugging or not... http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx[^]

It's complicated: Personally I would use the first version because it is easier to read, and to debug - but in practice it will make no difference at all! :laugh:


编译并查看!



您可以使用ildasm(中间语言反汇编程序)轻松比较编译的exe包含在Visual Studio中。



另外一个有趣的技巧是比较两个完全相同的程序,一个为Debug编译,另一个为Release编译。



就你的问题而言,如果你在发布模式下编译两个,结果代码应该是相同的。编译器将优化掉s变量并基本上在两者中产生完全相同的输出。
Compile it and see!

You can easily compare compiled exe's using ildasm (Intermediate Language Disassembler) that is included with Visual Studio.

Also another fun trick is to compare two of exactly the same programs, one compiled for Debug, and the other compiled for Release.

As far as your question goes, if you compile the two under Release mode, the resulting code should be the same. The compiler will optimize away the "s" variable and basically make exactly the same output in both.


除了其他答案:我建议你专注于代码维护和可读性并忽略这种小优化的可能性:通常,它们实际上不会优化任何东西。至于两个样本之间的可读性差异,没有明确的偏好。这取决于。第二种选择没有错,通常更好。首先是值得怀疑的,但这取决于具体情况。



我想说,优化代码的最佳方法是做任何愚蠢的事情。但是让我们看一下代码片段的语义。而且我讨厌发送:按名称调用方法的想法当然是决定性的,不可能的,不可能的愚蠢。我甚至不讨论这个想法,除非你解释你的目标并想讨论它。最好忘记它,永远不要做这些事情。



-SA
In addition to other answers: I would advice you to focus on code maintenance and readability and ignore the possibilities for such small "optimizations": most usually, they won't actually "optimize" anything. As to the readability differences between your two samples, there is no clear preferences. It depends. There is nothing wrong with second option, it's generally better. First is questionable, but it depends on the context.

I would say, the best way to optimize code is not doing anything stupid. But let's look at the semantics of your code fragment. And I hate to send: the idea calling a method by name is certainly, decisively, impossibly, impossibly silly. I don't even discuss this idea, unless you explain your goal and want to discuss it. Better forget about it and never do such things.

—SA


这篇关于编译器如何在C#中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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