Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Get Milliseconds in a timestamp

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Get Milliseconds in a timestamp

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.
--
Thanks,

Bruce Bolio


"Dave" wrote:

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Get Milliseconds in a timestamp

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

"Dave" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Get Milliseconds in a timestamp


I think the last line should be:

Test = timeTaken * 86400# * 1000#


--
Regards
Vergel Adriano


"Joel" wrote:

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

"Dave" wrote:

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Get Milliseconds in a timestamp

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

--
Regards,
Tom Ogilvy


"Bruce Bolio" wrote:

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.
--
Thanks,

Bruce Bolio


"Dave" wrote:

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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Get Milliseconds in a timestamp


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"
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
--
Regards,
Tom Ogilvy

  #7   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Get Milliseconds in a timestamp

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
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Get Milliseconds in a timestamp


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"
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
  #9   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Get Milliseconds in a timestamp

Thanks for correcting me, Jim!
GS

"Jim Cone" wrote:


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"
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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
hh:mm:ss AND Milliseconds? Lainey25 Excel Discussion (Misc queries) 5 May 6th 08 05:32 PM
Show Milliseconds Peter T Excel Programming 2 December 30th 06 02:39 PM
Show Milliseconds Jim Cone Excel Programming 0 December 28th 06 08:28 PM
Milliseconds in Excel dhg4 Excel Programming 3 April 12th 05 08:19 PM
Milliseconds In VB ccdubs Excel Programming 1 February 26th 04 11:20 PM


All times are GMT +1. The time now is 04:31 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"