1

什么将是内存消耗更少的设计?

我知道 ref 关键字通过引用传递参数,并且不会在函数参数中获取新的内存引用。我认为第一个设计将比第二个设计更有效。我对么?

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
   }

或 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
   }
4

1 回答 1

2

这是过早的优化。让我们先看看你的第一个代码优化了多少。

  1. List<T>并且string都是引用类型。因此,无论如何,它们在方法调用期间都不会被推入堆栈。因此,标记它们ref的参数不应该有任何积极的影响。

    private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
    
  2. 同样,List<T>是引用类型,而两个int变量都是值类型。因此,不将它们推入堆栈可能会有一些好处,但这将是微不足道的。

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

总而言之,好处可能是如此微不足道,以至于您最好将时间花在更关键和紧迫的问题上,而不是过于努力地优化它。

于 2018-02-26T05:22:08.450 回答