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

thisoldman

Well-known Member
Joined
Jan 5, 2014
Messages
1,075
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

Rich (BB 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:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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.
 
Upvote 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.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top