0

I made up two functions for illustration purpose. Could you suggest me which one is better practise?

Is Test2 is better for performance because it needs to declare less variables so may use less memory on the system?

Function Test1()

    Dim PerformMethod_A As Boolean = True
    Dim a = 1
    Dim b = 2
    Dim c = 3
    Dim d = 4
    Dim e = 5
    Dim result

    If PerformMethod_A Then
        result = a + b
    Else
        result = c + d + e
    End If

    Return result

End Function

Function Test2()

    Dim PerformMethod_A As Boolean = True
    Dim result

    If PerformMethod_A Then
        Dim a = 1
        Dim b = 2
        result = a + b
    Else
        Dim c = 3
        Dim d = 4
        Dim e = 5
        result = c + d + e
    End If

    Return result

End Function
Laurence
  • 7,633
  • 21
  • 78
  • 129
  • Duplicate of [Should variable declarations always be placed outside of a loop?](http://stackoverflow.com/questions/3241483/should-variable-declarations-always-be-placed-outside-of-a-loop) and others. Please do research yourself. – CodeCaster Aug 06 '13 at 10:22
  • Time it yourself running each one say 100,000 times and compare the results – Matt Wilko Aug 06 '13 at 10:30

2 Answers2

2

At this point you're performing micro performance enhancements. With the problem statement you've described there will be no noticeable difference either way. If you're having trouble with performance, you need to first gather metrics, so you know what to tune.

Read these articles by Eric Lippert, they will guide you.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 1
    Saying there isn't noticeable performance difference implies that there is a difference. If there is a difference that is only because the register allocation heuristics are tickled in a particular way which is something that you cannot control reliably anyway. What I am saying is that even if one needed micro perf, it would be a waste of time to move variable declarations around. It would be even more waste of time to run benchmarks on those changes. – Esailija Aug 06 '13 at 11:34
  • @Esailija, fully agreed. I think I was trying to say that if you could even find a difference it wouldn't matter. – Mike Perrenoud Aug 06 '13 at 11:36
1

If you have a look at what this compiles to in IL you'll see that it doesn't really make a difference. All local variables are actually declared at the top of the method in IL no matter where they are declared in the source.

Test1:

    // Method begins at RVA 0x2054
// Code size 59 (0x3b)
.maxstack 2
.locals init (
    [0] int32 a,
    [1] int32 b,
    [2] int32 c,
    [3] int32 d,
    [4] int32 e,
    [5] bool PerformMethod_A,
    [6] object result,
    [7] object Test1,
    [8] bool VB$CG$t_bool$S0
)

Test2:

// Method begins at RVA 0x209c
// Code size 58 (0x3a)
.maxstack 2
.locals init (
    [0] bool PerformMethod_A,
    [1] object result,
    [2] object Test2,
    [3] int32 a,
    [4] int32 b,
    [5] int32 c,
    [6] int32 d,
    [7] int32 e,
    [8] bool VB$CG$t_bool$S0
)
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49