简体   繁体   English

代码隐藏比内联代码更快吗?

[英]Is codebehind faster than inline code?

According to the question: Is C# code faster than Visual Basic.NET code ? 根据问题: C#代码是否比Visual Basic.NET代码更快 Was said that C# and VB.NET generates the same CLR code at the end. 据说C#和VB.NET最后生成了相同的CLR代码。

But in the case when I'm using codebehind and inline code, are there different performance (ignoring the language used)? 但是在我使用代码隐藏和内联代码的情况下,是否有不同的性能(忽略使用的语言)?

Inline code can require compilation the first time the request is made. 内联代码可能在第一次发出请求时需要编译。 After that (or if it's precompiled), there's absolutely zero difference between them. 在那之后(或者如果它是预编译的),它们之间绝对没有区别。

By the way, even if it requires compilation, the speed difference should be insignificant as ASP.NET will have to compile a source file anyway. 顺便说一句,即使它需要编译,速度差异应该是微不足道的,因为ASP.NET无论如何都必须编译源文件。 The difference will come down to a adding a few lines of code in a large source file! 差异将归结为在大型源文件中添加几行代码!

Yes, ish... If you're compiling at run-time you're always going to be more expensive than something which doesn't have to, but that compile will be cached (if you will) after the first request so you'll get zero difference from then on. 是的,是的......如果你在运行时进行编译,那么你总是会比不需要的东西更昂贵,但是在第一次请求之后编译将被缓存(如果你愿意)所以你从那时起,我将获得零差异。

There's probably somone who knows another reason, but to my mind the only realistic purpose for inline is the ability to make hot fixes without a rebuild + redeploy: the kind of thing you might do in small or early stage dev projects. 可能有人知道另一个原因,但在我看来,内联的唯一现实目的是能够在没有重建和重新部署的情况下进行热修复:你可以在小型或早期开发项目中做的事情。 Personally I also find inline just a little... aesthetically displeasing. 就个人而言,我也发现内联只是一点......美学上令人不悦。

The aspx pages have to be parsed and compiled anyway as ASP.Net turns them into classes that inherit from the codebehind (hence the inherit attribute in the page directive) so compilation is necessary in either case. 无论如何,ASP.Net将它们转换为从代码隐藏继承的类(因此是page指令中的inherit属性),因此必须对aspx页面进行解析和编译,因此在任何一种情况下都需要进行编译。 The difference between the two for first runs is going to be negligible unless we're talking about several thousand lines of code. 除非我们谈论几千行代码,否则第一次运行的两者之间的差异可以忽略不计。

But I agree with anna: inline is icky. 但我同意安娜:内联是icky。

I'm not sure whether the resulting assembly produced has the AllowOptimize attribute set to on or off. 我不确定生成的程序集是否将AllowOptimize属性设置为on或off。 I can find no documentation which indicates this either way. 我找不到任何表明这种方式的文件。

As such it is possible that the resulting code is not optimized by the JIT in quite the same way. 因此,JIT可能以完全相同的方式优化生成的代码。

I doubt this makes a significant difference if any (like I said this might be controlled in some other way) but certainly it could impact certain operations if it, for example, disabled inlining and you had a large, extremely tight loop. 我怀疑这会产生重大影响,如果有的话(比如我说过这可能会以其他方式控制),但肯定会影响某些操作,例如,禁用内联并且你有一个非常紧凑的大循环。 Such a construct would probably be a poor choice within an asp.net page so this shouldn't be an issue. 这样的构造在asp.net页面中可能是一个糟糕的选择,所以这应该不是问题。

No. Unless you are using web project the site needs to be compiled on the first hit. 不会。除非你使用的是web项目,否则网站需要在第一次点击时进行编译。 That affects inline as well as code behind to some extend. 这会在一定程度上影响内联和代码。 After that they run pretty much at the same speed. 之后他们几乎以相同的速度跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM