views:

159

answers:

1

In Visual Basic 2008, there's two different ways that I know of to accomplish the same thing:

The Dim on the member level:

Dim counter1 as integer = 0
Dim counter2 as integer = 180
Public Sub SampleSub1()
   Counter1 += 1 : If (Counter1 > 14) Then Counter1 = 0
   Counter2 += 1 : If (Counter2 > 240) Then Counter2 = 0
End Sub

Then there is the static on the procedure level:

Public Sub SampleSub2()
   Static Dim counter1 as integer = 0
   Static Dim counter2 as integer = 180
   Counter1 += 1 : If (Counter1 > 14) Then Counter1 = 0
   Counter2 += 1 : If (Counter2 > 240) Then Counter2 = 0
End Sub

I'm running a loop about 8 million times through this in about 7 seconds (with more data being processed), and using the static method on the counters actually takes about 500ms longer. Does the static method provide better memory management? Why is it slower?

Additionally, I declare my objects that are re-used with a dim on the member level or at least outside for loops, like:

Dim SampleObject as SampleClass
Public Sub SampleSub3()
   SampleObject = TheQueue.Dequeue()
End Sub

Should I use the Static method (which seems to be slower) on situations like this, or the dim on member level method that I already use?

I've reproduced the problem with this code in a test app:

Public Class Form1
Dim EndLoop As Integer = 50000000
Dim stopwatch1 As New Stopwatch

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    stopwatch1.Reset()
    stopwatch1.Start()

    For cnt As Integer = 1 To EndLoop
        test1()
    Next
    stopwatch1.Stop()

    Label1.Text = "Loop1: " & stopwatch1.ElapsedMilliseconds

    stopwatch1.Reset()
    stopwatch1.Start()
    For cnt As Integer = 1 To EndLoop
        test2()
    Next
    stopwatch1.Stop()

    Label2.Text = "Loop2: " & stopwatch1.ElapsedMilliseconds
End Sub
End Class

Public Module TestModule
Dim counter1 As Integer = 0
Dim counter2 As Integer = 180
Public Sub test1()
    counter1 += 1 : If (counter1 > 14) Then counter1 = 0
    counter2 += 1 : If (counter2 > 240) Then counter2 = 0
    COW1(counter1, counter2)
End Sub

Public Sub test2()
    Static counter3 As Integer = 0
    Static counter4 As Integer = 180
    counter3 += 1 : If (counter3 > 14) Then counter3 = 0
    counter4 += 1 : If (counter4 > 240) Then counter4 = 0
    COW1(counter3, counter4)
End Sub


Public Sub COW1(ByVal counter1 As Integer, ByVal counter2 As Integer)

End Sub
End Module

The slow down with the usage of the static variables does not occur unless I'm calling a sub from another module like the above example.

+5  A: 

Static local variables are a weird feature of VB.Net. When you compile them, they just get represented by a class level static variable behind the scenes.

However, if you're initializing and declaring a static local variable at the same time, what actually happens is the compiler sticks a Monitor class around it, to ensure that it can't be initialized by multiple threads at the same time.

It's the overhead of this Monitor that you're likely seeing. Try removing the initialization from the declarative statement and see if you notice a performance improvement.

womp
I get the same +500 milliseconds when I just do "Static Counter1 as integer = 0"
CowKingDeluxe
It appears that the slowdown only occurs when it's being used in a module. The example I just added works equal speed when the subs are contained in the form1 class, but a slowdown only on the static method occurs when I put the subs in a public module.
CowKingDeluxe
Static counter3 As Integer = 0 is still combining the variable declaration with an initialization statement.
womp
Here, take a look at this blog post by Patrick Steele - he covers it much more in depth: http://weblogs.asp.net/psteele/articles/7717.aspx
womp
Thanks womp, removing the =0 and setting it to 0 via another method fixed the speed slowdown. Thanks for the link too!
CowKingDeluxe