Get Milliseconds in a timestamp

D

Dave

I'm trying to figure out how long my macro is taking to run, and I want it as
accurate as possible... it would be great if I could get it down to
millisecond precision, is this possible?

Something like:

function test()
startTime = Now()

....
(function code)
....

endTime = Now()

timeTaken = endTime - startTime
test = timeTaken
end function
 
B

Bruce Bolio

You can get sickening accuracy.

The Time() function returns the current time.

If you do timeTaken = endTime - startTime, you get a result returned that
goes way past milliseconds.
 
J

Joel

I believe time is acurate to a timer tick in a PC and is in days. You have
to multiply by 3600 (days to seconds) and then by 1000 to get millisconds. I
added some dummy execution statements to show the accuracy of the timer. i
also declared variiables as double to get greatter precision.

Function Test() As Double

Dim Starttime As Double
Dim EndTime As Double

Starttime = Now()

Set MyRange = ActiveSheet.Range("C1:Z10000")

x = 0
For Each cell In MyRange
cell = 1234

Next cell
EndTime = Now()

timeTaken = EndTime - Starttime
'convert days to milliseconds
Test = timeTaken * 3600# * 1000#
End Function
 
J

Jim Cone

Tom,
For what its worth...
I believe that MS support article is not completely correct. The timer function
returns a Single not a Long and it appears the KB author may have used a
Long as the return value for the timer function and mislead himself.
In Ken Getz's VBA Developers Handbook, he states that the timer resolution
is about 1/18 of a second.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Tom Ogilvy" <[email protected]>
wrote in message
Bruce,
Microsoft appears to disagree. They say the resolution of the Timer, now
and Time are about 1 Second. They show other ways to time code:
http://support.microsoft.com/kb/172338
How To Use QueryPerformanceCounter to Time Code
 
G

GS

Hi Jim,

<AFAIK> Timer() returns a Double because you can break it down to a decimal
precision of a second. ie:

Sub TimeEvents()
' use to time procedures or functions
Dim StartTime As Date, EndTime As Date
StartTime = Timer

'Run procedure

EndTime = Timer
MsgBox Format(EndTime - StartTime, "0.00")
End Sub

You modify the format here to increase or decrease the number of decimal
places.

Regards,
Garry
 
J

Jim Cone

Sub TTT()
Dim strType As String
strType = TypeName(Timer)
MsgBox strType
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"GS" <[email protected]>
wrote in message
Hi Jim,
<AFAIK> Timer() returns a Double because you can break it down to a decimal
precision of a second. ie:

Sub TimeEvents()
' use to time procedures or functions
Dim StartTime As Date, EndTime As Date
StartTime = Timer

'Run procedure

EndTime = Timer
MsgBox Format(EndTime - StartTime, "0.00")
End Sub
You modify the format here to increase or decrease the number of decimal
places.
Regards,
Garry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top