2

I'm thinking more about how much system memory my programs will use nowadays. I'm currently doing A level Computing at college and I know that in most programs the difference will be negligible but I'm wondering if the following actually makes any difference, in any language.

Say I wanted to output "True" or "False" depending on whether a condition is true. Personally, I prefer to do something like this:

Dim result As String

If condition Then
   Result = "True"
Else
   Result = "False"
EndIf

Console.WriteLine(result)

However, I'm wondering if the following would consume less memory, etc.:

If condition Then
   Console.WriteLine("True")
Else
   Console.WriteLine("False")
EndIf

Obviously this is a very much simplified example and in most of my cases there is much more to be outputted, and I realise that in most commercial programs these kind of statements are rare, but hopefully you get the principle.

I'm focusing on VB.NET here because that is the language used for the course, but really I would be interested to know how this differs in different programming languages.

Andy
  • 3,600
  • 12
  • 53
  • 84
  • As said by dasblinkenlight, memory is not an issue nowadays unless under very specific situations (high-memory demanding complex system). In any case, you should focus your memory-maximisation efforts on other fronts which might get much more noticiable effects. The declaration of intermediate variables ("result" here) has virtually no impact at all (you are instantiating the given class, string; in the second code too while writing "True"/"False"). Some people tend to relate length of code with actual performance/memory utilisation, etc. and both situations are not related... – varocarbas Oct 06 '13 at 13:14
  • ... Intending to optimise the way in which you code (and building programs such that the memory expense is as low as possible) is undoubtedly a good thing, but you should focus on what really matters (do some research on highly-memory-consuming practices) and have one idea clear: from the size of the code you cannot get any valuable information; do some research or implement a method determining the memory consumption if you want to know how "expensive" is each bit. – varocarbas Oct 06 '13 at 13:20
  • @varocarbas Thank you for your comments. I see you points and I thought that would be the case regarding declaring intermediate variables... I know that the size of the example code means nothing is really relevant, however it was just to put across the concept of what I was trying to ask. – Andy Oct 09 '13 at 19:11

2 Answers2

3

The main issue making if's fast or slow is predictability.

Modern CPU's (anything after 2000) use a mechanism called branch prediction.
Read the above link first, then read on below...

Which is faster?
The if statement constitutes a branch, because the CPU needs to decide whether to follow or skip the if part.
If it guesses the branch correctly the jump will execute in 0 or 1 cycle (1 nanosecond on a 1Ghz computer).
If it does not guess the branch correctly the jump will take 50 cycles (give or take) (1/200th of a microsecord).

Therefore to even feel these differences as a human, you'd need to execute the if statement many millions of times.

The two statements above are likely to execute in exactly the same amount of time, because:

  1. assigning a value to a variable takes negligible time; on average less than a single cpu cycle on a multiscalar CPU*.
  2. calling a function with a constant parameter requires the use of an invisible temporary variable; so in all likelihood code A compiles to almost the exact same object code as code B.

*) All current CPU's are multiscalar.

Which consumes less memory
As stated above, both versions need to put the boolean into a variable.
Version A uses an explicit one, declared by you; version B uses an implicit one declared by the compiler.

However version A is guaranteed to only have one call to the function WriteLine.
Whilst version B may (or may not) have two calls to the function WriteLine.
If the optimizer in the compiler is good, code B will be transformed into code A, if it's not it will remain with the redundant calls.

How bad is the waste
The call takes about 10 bytes for the assignment of the string (Unicode 2 bytes per char).
But so does the other version, so that's the same.
That leaves 5 bytes for a call. Plus maybe a few extra bytes to set up a stackframe.
So lets say due to your totally horrible coding you have now wasted 10 bytes.

Not much to worry about.

From a maintainability point of view
Computer code is written for humans, not machines.
So from that point of view code A is clearly superior. Imagine not choosing between 2 options -true or false- but 20.
You only call the function once.
If you decide to change the WriteLine for another function you only have to change it in one place, not two or 20.

How to speed this up?
With 2 values it's pretty much impossible, but if you had 20 values you could use a lookup table.
Obviously that optimization is not worth it unless code gets executed many times.

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • +1 Thank you for answer. Branch prediction is very interesting and gives further evidence to support code A. Thanks for the food for thought – Andy Oct 09 '13 at 19:40
2

If you need to know the precise amount of memory the instructions are going to take, you can use ildasm on your code, and see for yourself. However, the amount of memory consumed by your code is much less relevant today, when the memory is so cheap and abundant, and compilers are smart enough to see common patterns and reduce the amount of code that they generate.

A much greater concern is readability of your code: if a complex chain of conditions always leads to printing a conditionally set result, your first code block expresses this idea in a cleaner way than the second one does. Everything else being equal, you should prefer whatever form of code that you find the most readable, and let the compiler worry about optimization.

P.S. It goes without saying that Console.WriteLine(condition) would produce the same result, but that is of course not the point of your question.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1 Thank your for your answer. I will take a look at `ildasm` but as it is really pointless with the example code I provided. – Andy Oct 09 '13 at 19:37