Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1692
  • Last Modified:

Batch program to measure the difference in time

how to convert startTIme in millisecond and get the difference :  endTime-startTime  and put the logic name and time taken in a log file.

ECHO OFF

cls
set "startTime=%time%"
SOME LOGIC
set "endTime=%time%"
echo %startTime%
echo %endTime%
set /A diff=(%enTime% - %starTime%)
echo %diff%
0
sakvk
Asked:
sakvk
1 Solution
 
Kakhaber SiradzeCommented:
ECHO OFF

cls
set "startTime=%time%"
SOME LOGIC
set "endTime=%time%"
echo %startTime%
echo %endTime%

set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)

set /A diff=%ENDTIME%-%STARTTIME%
echo %diff%
0
 
sakvkAuthor Commented:
this gives missing paranthesis..for some reason.. and converts to centiseconds.. not millisecidnds
0
 
Bill PrewCommented:
If you don't mind a tiny, free utility, I often use this tool for some basic batch timing needs.

http://www.gammadyne.com/cmdline.htm#timer

~bp
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
sakvkAuthor Commented:
humm. thanks, but i dont want the utility.
0
 
Bill PrewCommented:
Sadly there is no way to get down to millisecond precision from the DOS built in commands and variables.  You would either need to leverage a VBS script for that, or use a utility that can expose milliseconds to the BAT script.

~bp
0
 
sakvkAuthor Commented:
i just have  a starttime and end time , i want the difference of time. is it so difficult to achieve?
0
 
Bill PrewCommented:
Okay, not sure this will be enough for you, but here's how I would attack it based on what you have said.  Keep in mind though that since the precision of the %TIME% variable is only to hundredths of a second, the result will always be accurate only to 10's of milliseconds.  But the elapsed time will be presented as milliseconds.  Also keep in mind that no attempt has been made here to handle and end time that is less than a start time, I'm assuming for now that you will not be running this over a date change.

@echo off

REM Gather start and end times for comparison
set "startTime=%time%"
pause
set "endTime=%time%"

REM Convert HH:MM:SS.hh time formats to absolute milliseconds
call :Milli "%startTime%" "startMilli"
call :Milli "%endTime%" "endMilli"

REM Calculate elapsed milliseconds
set /A elapsMilli=%endMilli%-%startMilli%

REM Display results
echo %startTime%, %startMilli%
echo %endTime%, %endMilli%
echo %elapsMilli%

REM Exit main script
exit /b

REM Subroutine to convert time in HH:MM:SS,hh to milliseconds, return result in second parm
:Milli [HH:MM:SS.hh] [return-variable]	 
  for /F "delims=:. tokens=1-4" %%A in ("%~1") do set /A "%~2=(%%A*3600000)+((1%%B-100)*60000)+((1%%C-100)*1000)+(%%D*10)"
  exit /b

Open in new window

When I tested it here, waiting about 5 seconds to hit enter when the PAUSE executed, I got these results:
Press any key to continue . . .
 8:21:59.03, 30119030
 8:22:04.69, 30124690
5660

Open in new window

~bp
0
 
Bill PrewCommented:
Agreed, seems like it addressed the need of the poster.

~bp
0
 
QlemoC++ DeveloperCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now