Solved

Batch program to measure the difference in  time

Posted on 2011-03-03
10
1,381 Views
Last Modified: 2012-05-11
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
Comment
Question by:sakvk
10 Comments
 
LVL 5

Expert Comment

by:Angelgeo
ID: 35025040
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
 

Author Comment

by:sakvk
ID: 35025492
this gives missing paranthesis..for some reason.. and converts to centiseconds.. not millisecidnds
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35026437
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
 

Author Comment

by:sakvk
ID: 35028123
humm. thanks, but i dont want the utility.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 35029632
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
 

Author Comment

by:sakvk
ID: 35033971
i just have  a starttime and end time , i want the difference of time. is it so difficult to achieve?
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 350 total points
ID: 35036029
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35452913
Agreed, seems like it addressed the need of the poster.

~bp
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35481723
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now