Search code examples
c#pass-by-referencepass-by-valuememory-consumption

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. There for as my opinion first design will be more efficient than 2nd design. Am I correct?

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).

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
   }

Solution

  • This is premature optimization. Let's first see how much optimized your first code is.

    1. List<T> and string are both reference types. 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.

      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. 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.