Results 1 to 5 of 5

Thread: Variable Scope Reference

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Variable Scope Reference

    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

    Code:
        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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Variable Scope Reference

    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.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Variable Scope Reference

    How do you look at the IL from VB Express(2008)? I have never done that.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Variable Scope Reference

    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.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Variable Scope Reference

    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'
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width