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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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