Getting exact time (milliseconds or microseconds)

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
lsingbiel
Newbie
Posts: 3
Joined: Sat Mar 22, 2008 3:27 am
Location: Los Angeles
Contact:

Getting exact time (milliseconds or microseconds)

Post by lsingbiel » Thu Jun 11, 2009 4:25 am

Hi,

Does anyone know if it's possible to get the exact time in milliseconds or microseconds and store it into a variable? The only command I have been using is the "Sec>" command, but it returns the current number of seconds in whole seconds rather than milliseconds. Any help would be greatly appreciated. Thanks so much in advance!

~Len

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jun 11, 2009 3:42 pm

VBEval>Timer will give you seconds since midnight to a lot of decimal places. Obviously there are a lot of other issues you have to consider when trying to measure time this accurately on a PC.

VBSTART
VBEND
VBEval>Timer,decsec
MDL>decsec

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 11, 2009 6:11 pm

I could only think of two ways to get fractions of a second. Using the VBScript timer and using the TimeStamp> function. There might be a way using LibFunc> but I don't have time to research that option.

The following script runs for 5 seconds then pops the results into notepad. It simply loops and in each loop cycle uses the TimeStamp> function to write the cycle count and the VBScript "Timer" result to a file.

I found this test very revealing. Any machinist will immediately recognize the VBscript output as 64ths of a second. Oddly or maybe not so oddly that is the same interval that TimeStamp> reports.

I'm wondering if this is software specific, Operating System specific, or CPU chip specific? Can we really and accurately divide seconds any finer than 1/64 of a second? When I enter a wait>0.01 am I getting a 1/100th of a second wait or a 1/64th of a second wait?

Code: Select all

IfFileExists>%temp_dir%seconds.txt
  DeleteFile>%temp_dir%seconds.txt
EndIf

WriteLn>%temp_dir%seconds.txt,wres,DATESTAMP%TAB%%TAB%CYCLE%TAB%VBSCRIPT%crlf%

VBSTART
VBEND
VBEval>Timer,StartTime
Let>kk=0
Label>start
Add>kk,1
VBEval>Timer,thistime
DateStamp>%temp_dir%seconds.txt,%TAB%%kk%%TAB%%thistime%
VBEval>Timer-%StartTime%,TotalTime
If>TotalTime<5,start
Run>notepad.exe %temp_dir%seconds.txt

Exit>0

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Time to process a step

Post by gdyvig » Thu Jun 11, 2009 6:42 pm

Is the limitation the smallest time interval VBScript will report or is it how quickly Macro Scheduler and VBScript can process commands?

In your output is the same time reported for two or more report lines in a row?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jun 11, 2009 6:56 pm

Good catch Dick, Timer() does appear to have 1/64 second resolution.

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 11, 2009 7:03 pm

I'm pretty sure its the smallest time interval VBScript and Macro Scheduler will report. Possibly the smallest time interval the computer is capable of reporting.

The computer is writing about 19,500 lines in 5 seconds so each time interval displays on roughly 60 consecutive lines. For each written line 6 lines of Macro Scheduler script is processed. If my math is correct that's 360 lines of script processed each 1/64th of a second. I don't think script processing lag has anything to do with this.

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 11, 2009 8:32 pm

After some investigation I found that Windows Nt default timer resolution is 1/64th of a second. There are some multimedia timers available via Library functions. To investigate, start searching Microsoft's web site for "Windows NT High Resolution Timers".

Using The multimedia Library function "timeGetTime" we can retrieve millisecond time intervals. "timeGetTime" reports the number of milliseconds that have elapsed since Windows was started. Here is the above script with "timeGetTime" added to it.

Hope this is helpful.

Code: Select all

IfFileExists>%temp_dir%seconds.txt
  DeleteFile>%temp_dir%seconds.txt
EndIf

WriteLn>%temp_dir%seconds.txt,wres,DATESTAMP%TAB%%TAB%CYCLE%TAB%VBSCRIPT%TAB%timeGetTime%crlf%

VBSTART
VBEND
VBEval>Timer,StartTime
Let>kk=0
Label>start
Add>kk,1
VBEval>Timer,thistime
LibFunc>Winmm,timeGetTime,tGTres,
DateStamp>%temp_dir%seconds.txt,%TAB%%kk%%TAB%%thistime%%TAB%%tgtres%
VBEval>Timer-%StartTime%,TotalTime
If>TotalTime<5,start
Run>notepad.exe %temp_dir%seconds.txt

Exit>0

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 11, 2009 9:04 pm

After a reboot my computer is reporting even the "timeGetTime" results at only 1/64th of a second intervals. I don't understand why it WAS reporting correctly and after a reboot it is not but I thought I'd better mention it.

I'm baffled.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts