首页 新闻 会员 周边

彻底跪了。。求解释。

0
悬赏园豆:10 [已解决问题] 解决于 2013-10-27 16:38

环境:.net 4.0+ vs2012.3。

简单三层结构,二种写法的情能为什么会差别如此之大?

第一种写法:在index.aspx.cs中写以下方法“

protected List<NewChapterViewModel> newchapter(int count)
        {
            return new NewChapterBLL().GetList(count);
        }

在index.aspx前台页面使用for循环语句:

<% for (int i = 0; i < newchapter(40).Count; i++) { %>
    <%=newchapter(40)[i].AddDate%>
<%} %>


第二种写法:index.aspx.cs中

protected List<NewChapterViewModel> newchapter = new List<NewChapterViewModel>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                categorylist = new CategoryBLL().GetAllByParentID(0);
                newchapter = new NewChapterBLL().GetList(40);
            }
        }

index.aspx页面中

<% for (int i = 0; i < newchapter.Count; i++)
{ %>
   <li><b><%=newchapter[i].AddDate%></b></li>
<%}%


为什么第二种写法的性能要比第一种高好几倍?

同样是取40条内容,第二种方法能秒刷出来,第一种方法就需要10秒左右。

DZCMS的主页 DZCMS | 初学一级 | 园豆:5
提问于:2013-09-24 22:49
< >
分享
最佳答案
0

newchapter(40)函数调用,要去加载数据的。。。

收获园豆:10
sinhbv | 老鸟四级 |园豆:2579 | 2013-09-24 23:05
其他回答(1)
0

你难道没发现,你的第一种写法,要调用N次newchapter()方法吗?

幻天芒 | 园豆:37175 (高人七级) | 2013-09-25 00:15

谢谢上面二位提醒,断点走了一下,的确会走N次newchapter()方法。

而改用foreach速度就快了。

支持(0) 反对(0) DZCMS | 园豆:5 (初学一级) | 2013-09-25 06:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册