0

Let's say we got widnows batch file named program.bat with multiple lines of code. We need to measure execution time of variable blocks of this program.

Is there a simple way of determining time spend using small code chagnes ?

For example by modifying program.bat by adding call to function print_duration. Each time this function will be called it will print duration since start of the program.

call read_some_files
call print_duration
for ...
call process_files
call print_duration

with these two lines added to output:

Duration: 2 min
Duration: 8 min 
1
  • I use a hybrid batch file created by Dave Benham called GetTimeStamp. Commented Mar 3, 2020 at 14:11

1 Answer 1

1

As per your requirement. The below demonstrates how to use this. We call the start of the timer script before running the code, run the code, then call the end of the timer script. so for each command you will call start and end.

@echo off
call :start
echo run a command1 here.
call :end
call :start
echo run a command2 here.
call :end
goto :eof

:start
for /f "delims=," %%i in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"') do set start=%%i
for /f "delims=," %%i in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalMilliSeconds"') do set mstart=%%i
exit /b
:end
for /f "delims=," %%a in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"') do set end=%%a
for /f "delims=," %%a in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalMilliSeconds"') do set mend=%%a
set /a sres=1%end:~-2%-1%start:~-2%
set /a mres=1%mend:~-5%-1%mstart:~-5%%
echo                                                                                        Duration: %sres% sec %mres% ms
set start= | set mstart= | set end= | set mend=
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for sharing. However, this dosn't solve my problem because this launches shell where user types comands and gets results alongside with execution time. I need something that can be applied to existing bat script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.