简体   繁体   English

在C#中的循环内调用参数化方法的最佳方法是什么

[英]What is the best way to call a parameterized method inside a loop in C#

What will be the less memory consumable design?. 什么是更少的内存消耗设计?

I know that ref keyword passes arguments by reference & will not take a new memory reference inside the function arguments. 我知道ref关键字通过引用传递参数&不会在函数参数内部使用新的内存引用。 There for as my opinion first design will be more efficient than 2nd design. 我认为第一个设计将比第二个设计更有效率。 Am I correct? 我对么?

1). 1)。

public void SyncWeighEntityData(string sqlConnectionString, int weighBridgeID, string weibridgeName, int lastSyncedTicetNo, string imagesPath)
   {
       using (var cn = DBUtils.GetNewOpenConnection(sqlConnectionString))
       {

           var query = "SELECT * FROM [WeighEntry] WHERE TicketNo > @TicketNo";
           var lastTicketNo = cn.Query<int>("SELECT MAX(TicketNo) FROM [WeighEntry]").Single();
           var results = cn.Query(query, new { TicketNo = lastSyncedTicetNo }).ToList();
           results.ForEach(x => x.WeighBridgeID = weighBridgeID);
           cn.Close();


          SyncImages(ref results, ref imagesPath, ref weibridgeName);
           SyncWeighEntityDataToLocalDB(ref results, ref  lastTicketNo, ref weighBridgeID);
       }

   }

   private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
   {
       foreach (var item in WeightEntitiesToSync)
       {
           for (int i = 0; i < 4; i++)
           {
              SaveTicketImageOnLocalPath(Path.GetFullPath(CommonResources.DecodeFromBase64(imagesPath) + string.Format($"\\{item.TicketNo}_CropedIn.Jpg")), item.WeighBridgeID, weibridgeName, string.Format($"{item.TicketNo}_CropedIn.Jpg"));
           }
       }
   }

   private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID)
   {
      // Code to save data
   }

or 2). 或2)。

public void SyncWeighEntityData(string sqlConnectionString, int weighBridgeID, string weibridgeName, int lastSyncedTicetNo, string imagesPath)
   {
       using (var cn = DBUtils.GetNewOpenConnection(sqlConnectionString))
       {

           var query = "SELECT [TicketNo],[VihNo],[Supervisor],[Driver],[Cleaner],[SealNO1],[SealNO2],[SealNO3],[ContainerNo],[ItemDis],[TimeIn],[TimeOut],[Weigh1],[Weigh2],[Remarks1st],[Remarks2ed],[CustName],[DateIn],[DateOut],[GinorGrnNo],[UserName],[AccountBillCount],[StoresBillCount],[CustomerBillCount] FROM [WeighEntry] WHERE TicketNo > @TicketNo";
           var lastTicketNo = cn.Query<int>("SELECT MAX(TicketNo) FROM [WeighEntry]").Single();
           var results = cn.Query(query, new { TicketNo = lastSyncedTicetNo }).ToList();
           results.ForEach(x => x.WeighBridgeID = weighBridgeID);
           cn.Close();


          SyncImages( results,  imagesPath,  weibridgeName);
           SyncWeighEntityDataToLocalDB( results,   lastTicketNo,weighBridgeID);
       }

   }

   private void SyncImages( List<dynamic> WeightEntitiesToSync,  string imagesPath,  string weibridgeName)
   {
       foreach (var item in WeightEntitiesToSync)
       {
           for (int i = 0; i < 4; i++)
           {
              SaveTicketImageOnLocalPath(Path.GetFullPath(CommonResources.DecodeFromBase64(imagesPath) + string.Format($"\\{item.TicketNo}_CropedIn.Jpg")), item.WeighBridgeID, weibridgeName, string.Format($"{item.TicketNo}_CropedIn.Jpg"));
           }
       }
   }

   private void SyncWeighEntityDataToLocalDB( List<dynamic> WeightEntitiesToSync,  int lastTicketNo,  int weighBridgeID)
   {
      // Code to save data
   }

This is premature optimization. 这是过早的优化。 Let's first see how much optimized your first code is. 我们首先来看一下您的第一个代码的优化程度。

  1. List<T> and string are both reference types. List<T>string都是引用类型。 So in any event they are not pushed to stack during the method call. 因此,无论如何在方法调用期间都不会将它们压入堆栈。 Hence marking them ref parameters shouldn't have any positive effect. 因此,将它们标记为ref参数不会产生任何积极效果。

     private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName) 
  2. Again, List<T> is a reference type, while the two int variables are value type. 同样, List<T>是引用类型,而两个int变量是值类型。 So there could be some benefit by not pushing them onto stack, but it would be insignificant. 因此,不将它们压入堆栈可能会有一些好处,但这并不重要。

     private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID) 

To sum it up, the benefits might be so trivial that you are better off spending your time on more critical and pressing problems, rather than trying too hard to optimize this. 综上所述,这样做的好处可能微不足道,以至于您最好将时间花在更严重和紧迫的问题上,而不要花太多时间来优化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM