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%
sakvkAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
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:
Agreed, seems like it addressed the need of the poster.

~bp
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.