当前位置:编程学习 > C#/ASP.NET >>

循环内,循环外实例化对象的效率问题。。

在一个函数体内

function aa ()
{
 for (int i = 0; i < dataTable.Count; i++)
 {
   var conn1 = CData.TableAdaptorHelper.GetConnection(DB_SCHEMA);
                               using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.RequiresNew))
                            {
                                conn1.Open();

                                A0TableAdapter A0 = new A0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                A0DataTable A0DataTable = new DataSet.A0DataTable();

                                B0TableAdapter b0 = new B0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                B0DataTable b0DataTable = new DataSet.B0DataTable();

                                C0TableAdapter c0 = new C0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                C0DataTable c0DataTable = new DataSet.C0DataTable();

                                D0TableAdapter d0 = new D0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                D0DataTable d0DataTable = new DataSet.D0DataTable();

                                E0TableAdapter A0 = new E0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                E0DataTable e0DataTable = new DataSet.E0DataTable();
                                
                                ..........
                           }
  }
}


function aa ()
{

 var conn1 = CData.TableAdaptorHelper.GetConnection(DB_SCHEMA);
  A0TableAdapter A0 = new A0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                A0DataTable A0DataTable = new DataSet.A0DataTable();

                                B0TableAdapter b0 = new B0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                B0DataTable b0DataTable = new DataSet.B0DataTable();

                                C0TableAdapter c0 = new C0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                C0DataTable c0DataTable = new DataSet.C0DataTable();

                                D0TableAdapter d0 = new D0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                D0DataTable d0DataTable = new DataSet.D0DataTable();

                                E0TableAdapter A0 = new E0TableAdapter()
                                {
                                    Connection = conn1
                                };
                                E0DataTable e0DataTable = new DataSet.E0DataTable();
 for (int i = 0; i < dataTable.Count; i++)
 {
  
                             using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.RequiresNew))
                            {
                                conn1.Open();

                               
                                
                                ..........
                           }
  }
}


两种方法实列化效率上有啥区别哇》??? 请教各位。。。。
--------------------编程问答-------------------- 循环内,实例一次又一次 --------------------编程问答--------------------
引用 1 楼 Imaor 的回复:
循环内,实例一次又一次

但是只是在循环体内有效, 出了循环体那个变量就不存在了啊,只是内存有没有及时回收,不是很清楚。。。
--------------------编程问答-------------------- 你把这段也放到循环外就可以了:
using (TransactionScope scope1 = new TransactionScope
--------------------编程问答--------------------
引用 3 楼 rtdb 的回复:
你把这段也放到循环外就可以了:
using (TransactionScope scope1 = new TransactionScope


额,这个是我的疏忽, 效率上是不是放在循环体外效率高的多?
--------------------编程问答-------------------- 都一样吧,都要创建新的对象,对象不用,就抛弃了,但是等待垃圾回收 --------------------编程问答-------------------- 但是你这种写法,放到循环外边,就是一个对象,循环内,就是多个对象了,放到循环外边好 --------------------编程问答-------------------- --------------------编程问答-------------------- 自己运行下比较下时间就知道了。应该差别不大。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,