1

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.

2
  • The code is not the same, you'd have to use & instead of &&. The usual problem with micro-optimizations, they tend to add bugs before they add speed. Aug 15, 2014 at 10:20
  • @HansPassant why? would be awesome if you could point me to (or add a ) good explanation :) Aug 15, 2014 at 10:56

2 Answers 2

2

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.

0
1

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.

1
  • thanks. I love to challenge my assumptions and learn at the same time :) Aug 15, 2014 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.