Paul D'Ambra
Paul D'Ambra

Reputation: 7814

Does this optimisation reduce memory use?

Take for (a contrived) example

private bool CheckSomeThings() 
{
  var importantThing = GetImportantThing();
  var generatedOtherThing = DoFunkyStuff(importantThing);

  bool firstCheck = CheckThing(importantThing);
  bool otherCheck = IsItStuff(generatedOtherThing);
  bool furtherStuff = FurtherChecking(importantThing, generatedOtherThing);

  return firstCheck && otherCheck && furtherStuff;   
}

When writing/reviewing code like this I tend to suggest not creating the bool just to use it as a check so I prefer

private bool CheckSomeThings() 
{
  var importantThing = GetImportantThing();
  var generatedOtherThing = DoFunkyStuff(importantThing);

  return CheckThing(importantThing) 
    && IsItStuff(generatedOtherThing) 
    && FurtherChecking(importantThing, generatedOtherThing);   
}

I find the second example more readable because I don't have to parse the created variables and check if they are used anywhere... but I recognise that's subjective.

However, I think that the first example (with the additional assignments) uses more memory than the second. So makes an easy win.

While premature optimisation is akin to punching a puppy the real code that lead to this question is going to be running as part of a real-time processing doohickie so optimising memory use is likely to be a real concern.

I wondered if I'm right about the memory use as a result of the assignments?


Also, yes we will run the system through a memory profiler etc to get real data about how it performs this is a question about whether a simple code style approach can protect against unnecessary memory use.

Upvotes: 1

Views: 47

Answers (2)

DoctorMick
DoctorMick

Reputation: 6793

Assigning to the two extra variables will use a very small amount of additional memory but for me that isn't relevant unless you have really, really, really strict memory usage requirements as it'll be released soon enough.

The more important aspect is that if you assign the variables you'll have to call IsItStuff and FurtherChecking regardless of whether CheckThing passes or fails so you're doing unnecessary processing which is usually more important as it'll take longer for the function to complete.

If you use the second example the if statement will be short circuited and won't call any of the functions after the first false is returned so it will be more efficient whenever a check fails.

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 156968

DoctorMick already did a good job explaining your 'optimization' is actually not a optimization.

Even if you would write such code and use properties or variables only for example (so you wouldn't use expensive method calls), the optimizer would inline that, so the gain would be nothing.

So this:

bool x = SomeVariable;

if (x)

Will be optimized by the compiler to:

if (SomeVariable)

Note that this is only possible if you don't reassign x later on in the code.

Upvotes: 1

Related Questions