内存性能的最佳实践
Best practices for memory performance
我有以下代码。我们争论过哪种方法对内存性能更好。如果我们不使用单独的变量并使用它,它会有什么不同吗?它会影响那么这是可能的解决方案吗?
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
chkTreatSR.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
或
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
var values = value.Split(',');
chkTreatSR.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
谢谢。
第二个示例将分配较少的内存,因为每次调用 string.Split(,) 都会分配一个新的字符串数组。
如果这是您代码中的 "hot-spot",您可能会考虑根本不调用 string.Split,而是使用 Regex 来搜索您的枚举。
我有以下代码。我们争论过哪种方法对内存性能更好。如果我们不使用单独的变量并使用它,它会有什么不同吗?它会影响那么这是可能的解决方案吗?
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
chkTreatSR.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
或
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
var values = value.Split(',');
chkTreatSR.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
谢谢。
第二个示例将分配较少的内存,因为每次调用 string.Split(,) 都会分配一个新的字符串数组。
如果这是您代码中的 "hot-spot",您可能会考虑根本不调用 string.Split,而是使用 Regex 来搜索您的枚举。