PDA

Click to See Complete Forum and Search --> : Variable Scope Reference


dbasnett
Mar 28th, 2009, 02:16 PM
I am looking for a reference that explains how/why the scope of a variable affects performance. Here is an example of what I mean

03/28/09 13:12:51.516 CPU - 1.794GHz. Stopwatch(HR) Freq. is 3,579,545
x86 Family 6 Model 13 Stepping 6 Address width - 32 Memory - 2.0GB

One - Procedure
Two - Module
Outer Loops(OL) = 7 Inner Loops(IL) = 262,144
Times given in ticks. 1 ms. = 10,000 ticks; 1,000 ms. = 1 second.

Ticks / Loop Fastest? One
0.0194 1.2242 1.7381 -0.5139
OL Base IL One IL Two IL Difference (One - Two)
1 5,055 320,265 454,974 -134,709
2 5,086 320,411 454,639 -134,228
3 5,058 320,113 455,677 -135,564
4 5,093 320,022 455,077 -135,055
5 5,057 324,892 459,133 -134,241
6 5,247 319,998 455,082 -135,084
7 5,057 320,722 454,889 -134,167

Average
5,093 320,917 455,638 -134,721 (-13.472 ms./IL)

03/28/09 13:12:53.113 RunMode = CTRL-F5 End Dewayne Relative Speed Test
Source Code Follows
'
Const itms As Integer = 100
Dim TestData() As Integer
Dim TestList As New List(Of Integer)
Dim sum As Integer = 0
'
Private Function TestCase1() As Boolean 'One
'Test One Code Follows
Dim sum As Integer = 0
For i As Integer = 0 To TestData.Length - 1
sum += TestData(i)
Next
'End Test One Code
Return True
End Function
'
Private Function TestCase2() As Boolean 'Two
'Test Two Code Follows
sum = 0
For i As Integer = 0 To TestData.Length - 1
sum += TestData(i)
Next
'End Test Two Code
Return True
End Function
'
Private Sub _init()
'Code needed to get the Test to run. NOT part of the timing.
'Only called once!!!
TestList.Clear()
For x As Integer = 1 To itms
TestList.Add(Short.MaxValue)
Next
TestData = TestList.ToArray
End Sub

Shaggy Hiker
Mar 28th, 2009, 07:25 PM
What does the IL show you?

In one case you have a variable that is created on the stack and may well be retained in either the processor registers, or the cache, without having any real impact outside of the routine, while in the other case you have a variable created on the heap, which may have a read/write to system RAM for every iteration.

dbasnett
Mar 29th, 2009, 08:23 AM
How do you look at the IL from VB Express(2008)? I have never done that.

Shaggy Hiker
Mar 29th, 2009, 10:14 AM
Neither have I, since I don't use Express.

Might as well do a search to see if ILDASM is included on your computer. I would expect that it is there, but I never remember where it is located. I add it to the Tools menu with Tools|External Tools..., but I think that just lets you browse for tools, so you need to know where ILDASM is located, first.

dbasnett
Mar 29th, 2009, 10:27 AM
Found it under 2003 SDK. I tried opening .exe's and it gave me an error "Failed to open meta data". Sounds like a title for a song 'I lost my meta data'