性能ASP.NET MVC 4反光效果 [英] ASP.NET MVC 4 Reflection effect on performance

查看:215
本文介绍了性能ASP.NET MVC 4反光效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的ASP.NET MVC的发展,开始4天前。你可以说只是在研发和D阶段。我喜欢MVC发展的风格,但我的困惑开始的时候,我的一个朋友告诉我说,ASP.NET MVC的表现不符合,因为使用反射的WebForms的可比性。例如:

I am very new to ASP.NET MVC development, started 5 days ago. You can say just in R&D phase. I like the style of development of MVC but my confusion started when one of my friend told me that ASP.NET MVC's Performance is not comparable with WebForms because of use of Reflection. e.g.:

@Html.EditorFor(model => model.FieldName)

MVC是使用反射。因此,据他说,我们应该用正常的HTML标签,克服了反映。例如,

MVC is using reflection. So according to him, we should use normal HTML Tags to overcome the reflection. e.g.

<input id="FieldName" class="text-box single-line" type="text" value="" name="FieldName" data-val-required="FieldName is required." data-val="true" />

我试图混淆除去通过搜索在互联网上,找到以下内容:

I tried to remove by confusion by searching over the internet and found following :

ASP.NET MVC性能
<一href="http://stackoverflow.com/questions/14768808/why-use-lambdas-in-asp-net-mvc-instead-of-reflection">Why使用,而不是反映lambda表达式在ASP.NET MVC?
在网络 反思与性能

ASP.NET MVC Performance
Why use lambdas in ASP.NET MVC instead of reflection?
Reflection and performance in web

但这些话题并没有跟我说清楚是否使用正常的HTML会给我更多的性能或没有,这将是在MVC中大规模应用的最佳方法。

But these topics does not tell me clearly whether using normal HTML will give me more performance or not and what will be the best method in MVC large scale application.

推荐答案

正在无形中保证了无反射的版本会更快,因为它需要做的工作更少。然而,我预期的差值是相当小的。我做了一些基本的测试有两个不同的模板。

You are virtually guaranteed that the non-reflection version will be faster as it needs to do less work. However, I expected the difference to be quite small. I have done some basic tests with two different templates.

1的模板使用HTML助手:

Template 1 using Html helpers:

@for (int i = 0; i < 10000; i++)
{
    @Html.EditorFor(model => model.FieldName)  
}

模板2采用纯HTML:

Template 2 using plain HTML:

@for (int i = 0; i < 10000; i++)
{
    <input id="FieldName" class="text-box single-line" type="text" value="" name="FieldName" data-val-required="FieldName is required." data-val="true" />
}

要测试的表现,我已经使用了 MiniProfiler 库,它给了我们多少时间花在呈现一个想法的模板。当平均的结果,我得到了以下值:

To test the performance, I have used the MiniProfiler library which gives us an idea on how much time is spent rendering the templates. When averaging the results, I get the following values:

  • 在模板1(与HTML佣工): 405.8毫秒
  • 在模板2(普通HTML): 82.1毫秒
  • Template 1 (with HTML helpers): 405.8 ms
  • Template 2 (plain HTML): 82.1 ms

的区别是这样的 323.7毫秒,一个5倍的差异。但是,我们使用10000次迭代这可能不是默认行为模板进行我们的测试。如果我们改变模板到一个更合理的默认像10倍,我们得到如下结果:

The difference is thus 323.7 ms, a factor 5 difference. However, we have done our test using 10000 iterations which probably isn't default behavior in a template. If we change the templates to a more sensible default like 10 times, we get the following results:

  • 在模板1(与HTML佣工): 3.7毫秒
  • 在模板2(普通HTML): 0.5毫秒
  • Template 1 (with HTML helpers): 3.7 ms
  • Template 2 (plain HTML): 0.5 ms

现在你仍然可以看到一些差异( 3.2毫秒),但在我的愚见,这是可以忽略不计。在这一点上,我不会看的性能几乎只能看什么风格,你preFER。如果你想完全控制你的HTML,去为普通的HTML版本。如果你想拥有所有的临时演员的HTML辅助提供(如自动翻译注释jQuery的验证属性),去的方法。这两种方法之间的差异将在大多数情况下是足够低,以不会有大的影响。

Now you can still see some difference (3.2 ms), but in my humble opinion this is negligible. At this point, I would not be looking at the performance at all but only look at what style you prefer. If you want to completely control your HTML, go for the plain HTML version. If you want to have all the extras the HTML helpers offer (like automatically translating annotations to jQuery validation attributes), go for that method. The difference between the two methods will in most cases be low enough to not have a large impact.

这篇关于性能ASP.NET MVC 4反光效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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