Solved

Batch program to measure the difference in  time

Posted on 2011-03-03
10
1,399 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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

13 Experts available now in Live!

Get 1:1 Help Now