0

I have the below code. We had an argument regarding which approach is better for memory performance. Will it make any difference at all if we don't use a separate variable and use it? It it will affect then is this the possible solution ?

    /// <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;
        }
    }

OR

    /// <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;
        }
    }

Thanks.

user3590485
  • 241
  • 1
  • 9
  • 19
  • 3
    Test, test, benchmark and test. That's the only was you will get a concrete answer. Other than that, best practice questions are off topic here. – DavidG Jun 08 '17 at 22:21
  • If you write "stringly typed" code it is generally a bit too early to worry about performance... Also normally one would expect different behavior from properties - `x.p = v; AssertTrue(x.p == v)` for all `v` vs. code shown in the post that does some strange conversion and rarely satisfy condition that setting property changes its value. – Alexei Levenkov Jun 08 '17 at 22:29
  • 1
    The second snippet is a no-brainer in my opinion. Splitting the same string the same way 5 times instead of 1 seems ridiculous to me. – itsme86 Jun 08 '17 at 23:13

1 Answers1

0

The second example will have less memory allocation as each call to string.Split(,) will allocate a new string array.

If this is a "hot-spot" in your code, you might consider not calling string.Split at all, and instead use Regex to search for your enums.

Daniel Keogh
  • 233
  • 1
  • 7