在 C# 的循环中调用参数化方法的最佳方法是什么
What is the best way to call a parameterized method inside a loop in C#
内存消耗更少的设计是什么?
我知道 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
}
这是过早的优化。让我们先看看你的第一个代码优化了多少。
List<T>
和 string
都是引用类型。所以在任何情况下,它们都不会在方法调用期间被压入堆栈。因此标记它们 ref
参数不应该有任何积极影响。
private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
同样,List<T>
是引用类型,而两个int
变量是值类型。因此,可以通过不将它们压入堆栈来获得一些好处,但这微不足道。
private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID)
总而言之,好处可能微不足道,您最好将时间花在更关键和紧迫的问题上,而不是努力优化它。
内存消耗更少的设计是什么?
我知道 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
}
这是过早的优化。让我们先看看你的第一个代码优化了多少。
List<T>
和string
都是引用类型。所以在任何情况下,它们都不会在方法调用期间被压入堆栈。因此标记它们ref
参数不应该有任何积极影响。private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
同样,
List<T>
是引用类型,而两个int
变量是值类型。因此,可以通过不将它们压入堆栈来获得一些好处,但这微不足道。private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID)
总而言之,好处可能微不足道,您最好将时间花在更关键和紧迫的问题上,而不是努力优化它。