2009-11-10 26 views
5

Asp.net Mvc 2を使用している私の最近のプロジェクトでは、DisplayForにパフォーマンスの問題があることがわかりました。 本物かどうか分からないのですか?Asp.net MVC 2 DisplayForパフォーマンスの問題?

私はいくつかのAsp.net Mvc Guruが私に説明できることを願っています。 :)

モデル。

public class Customer 
{ 
    public int CustomerId { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string EmailAddress { get; set; } 

    public static IEnumerable<Customer> GetCustomers() 
    {    
     for (int i = 0; i < 1000; i++) 
     { 
      var cust = new Customer() 
      { 
       CustomerId = i + 1, 
       Name = "Name - " + (i + 1), 
       Address = "Somewhere in the Earth...", 
       EmailAddress = "customerABC" 
      }; 

      yield return cust; 
     } 
    } 
} 

コントローラ

public ActionResult V1() 
    {    
     return View(Customer.GetCustomers()); 
    } 

    public ActionResult V2() 
    { 
     return View(Customer.GetCustomers()); 
    } 

(パフォーマンスの問題を有している)V1

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    V1 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>V1</h2> 
    <table> 
    <%foreach (var cust in this.Model) 
     {%> 
     <%= Html.DisplayFor(m => cust) %> 
     <%} %> 
    </table> 
</asp:Content> 

テンプレートが

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Customer>" %> 
<tr> 
    <td><%= this.Model.CustomerId %></td> 
    <td><%= this.Model.Name %></td> 
    <td><%= this.Model.Address %></td> 
    <td><%= this.Model.EmailAddress %></td>  
</tr> 

あるV2(NOパフォーマンスの問題)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    V2 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2>V2</h2> 
    <table> 
    <%foreach (var cust in this.Model) 
     {%> 
     <tr> 
      <td><%= cust.CustomerId%></td> 
      <td><%= cust.Name%></td> 
      <td><%= cust.Address%></td> 
      <td><%= cust.EmailAddress%></td>  
     </tr> 
     <%} %> 
     </table> 
</asp:Content> 

V1とV2のパフォーマンスの違いを簡単に確認できます。

EDIT:ローカルのIIS 7(リリースバージョン)に展開すると、(V1)が非常に高速になります。問題は解決しましたが、私はまだその理由を知りたいです。 :)

おかげで、
ソー萌

答えて

12

キャッシュは、リリースモードでのみ有効です。デバッグモードでアプリケーションを実行すると、ディスクアクセスのためにパフォーマンスが低下することがあります。

も参照してください: http://codeclimber.net.nz/archive/2009/04/22/how-to-improve-htmlhelper.renderpartial-performances-donrsquot-run-in-debug-mode.aspx

+0

ありがとう!私はデバッグをfalseに切り替え、パフォーマンスは私が期待したものに戻りました... – bytebender

0

は問題がDisplayFor()が実行時にコンパイルされて実行されるラムダ式を使用することです。

したがって、V1のパフォーマンスの違いは、この「中間」コンパイルステップに起因する可能性があります。

V2は単にコンパイルを必要としないプロパティアクセスです。

私はここで推測していますが、IIS7は将来の再利用のためにビューのキャッシュされたコピー(およびコンパイル済みのラムダ式)を保持するほどスマートだと想像します。 IISのV1。

+0

IIS7は、ここで重要な要因であることを前提として、正しくありません。正しい情報については、Leviの答えをご覧ください。 – thomasjo

+0

答えが受け入れられた後、私はそれを削除できないので、IIS7が言及されました:( – Codebrain