Results 1 to 3 of 3

Thread: VBA: C.Williams' MicroTimer Why include the line 'MicroTimer= 0'

  1. #1
    Board Regular
    Join Date
    Jan 2014
    Location
    Pittsburgh
    Posts
    1,073
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    1 Thread(s)

    Default VBA: C.Williams' MicroTimer Why include the line 'MicroTimer= 0'

    I'm sure many of you are familiar with the MicroTimer function by Charles Williams. A copy can be found here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/excel-improving-calcuation-performance#measuring-calculation-time

    I'm curious about why the setup for the function calls and calculation includes the line:
    MicroTimer = 0

    Code:
    [✂ --Snip-- ✂]
    
    Function MicroTimer() As Double
    '
    ' Returns seconds.
        Dim cyTicks1 As Currency
        Static cyFrequency As Currency
        '
        MicroTimer = 0        ' ← Why this line?
    
    ' Get frequency.
        If cyFrequency = 0 Then getFrequency cyFrequency
    
    [✂ --Snip-- ✂]
    It seems to me that the line is unneeded. When I use the function, there seems to be no discernible difference in results when the line is included, commented out, or deleted. Could it be that setting the value consumes a few less computation cycles compared to when it is set by default?
    Last edited by thisoldman; May 7th, 2018 at 09:18 AM.

  2. #2
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,628
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: VBA: C.Williams' MicroTimer Why include the line 'MicroTimer= 0'

    This is largely a matter of style. In VBA, numeric variables are automatically initialized to zero upon first use. String variables to "", and other variables similarly. So yes, in VBA, you could omit that line and it would work.

    However, in other languages, that is not always the case. Sometimes the variable is undefined, or has random garbage in it, so initializing the variable is essential. I typically initialize variables in VBA, partly for consistency with other languages I use, and partly for self-documentation (so I can see what it starts out as), and partly because I don't always want it to start at zero. A row counter might start at 1 instead.

    That's my 2 cents worth.
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  3. #3
    Board Regular
    Join Date
    Jan 2014
    Location
    Pittsburgh
    Posts
    1,073
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA: C.Williams' MicroTimer Why include the line 'MicroTimer= 0'

    Thank you, Eric. That was my suspicion. I am all for eliminating lines of code, especially for a language that I find to be verbose. I would have voted for implied over explicit in this script.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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