Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top