匿名类和 IDictionary速度差异对于 ASP.NET MVC 中的 htmlAttributes
我正在尝试使用一些技术来优化我的 ASP.NET MVC 应用程序,其中包括 URL 生成调整: http://www.chadmoran.com/blog/2009/4/23/optimizing-url- Generation-in-aspnet-mvc- part-2.html
如果使用 RouteValueDictionary 代替匿名类之间的速度差异如此之大,那么在定义 html 属性时我是否也应该考虑使用 Dictionary 代替匿名类?
例如,我应该这样做:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new { @class = "someCSSClass" })
还是应该通过这样做进一步优化:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new Dictionary<string, object> { { "class", "someCSSClass" } })
我知道使用 Url.Action 更快,或者使用 RouteLink 技术更好,但我只是想知道是否应该完全避免匿名类为了速度。
I am trying to optimize my ASP.NET MVC application using some techniques that include URL generation tweaking a la: http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html
If the speed difference is so large between using RouteValueDictionary in place of anonymous classes, then should I also look to use Dictionary in place of anonymous classes when defining html attributes?
For example, should I do this:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new { @class = "someCSSClass" })
or should I further optimize by doing this:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new Dictionary<string, object> { { "class", "someCSSClass" } })
I know it's even faster to use Url.Action, or better yet to use the RouteLink technique, but I'm just wondering whether anonymous classes should be completely avoided for the sake of speed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用词典更快。
它的速度是否足以带来改变?对于您的应用程序,只有分析器才能告诉您这一点。我建议,如果它确实产生了影响,那么您无论如何都应该缓存您的视图结果。
不过,我倾向于坚持使用字典版本,因为强类型有助于消除
ActionLink
的疯狂重载。传递对象
很容易导致错误的重载。速度只是一个奖励。Yes, it's faster to use the Dictionary.
Is it fast enough to make a difference? Only a profiler can tell you that, for your application. I'd suggest that if it actually makes a difference then you should be caching your view results anyway.
I tend to stick with the Dictionary versions, though, because the strong typing helps to cut through the insane mess of overloads to
ActionLink
. Passing anobject
makes it way too easy to end up with the wrong overload. The speed is just a bonus.