是否有一种内置的方法来测量Windows命令行上命令的执行时间?


当前回答

用潜水艇返回百分之一秒的时间

::tiemeit.cmd
@echo off
Setlocal EnableDelayedExpansion

call :clock 

::call your_command  or more > null to pipe this batch after your_command

call :clock

echo %timed%
pause
goto:eof

:clock
if not defined timed set timed=0
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do ( 
set /A timed = "(((1%%a - 100) * 60 + (1%%b - 100)) * 60 + (1%%c - 100))  * 100 + (1%%d - 100)- %timed%"
)
goto:eof

其他回答

用潜水艇返回百分之一秒的时间

::tiemeit.cmd
@echo off
Setlocal EnableDelayedExpansion

call :clock 

::call your_command  or more > null to pipe this batch after your_command

call :clock

echo %timed%
pause
goto:eof

:clock
if not defined timed set timed=0
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do ( 
set /A timed = "(((1%%a - 100) * 60 + (1%%b - 100)) * 60 + (1%%c - 100))  * 100 + (1%%d - 100)- %timed%"
)
goto:eof

呵呵,最简单的解决办法可能是:

echo %time%
YourApp.exe
echo %time%

这适用于所有开箱即用的Windows。


在应用程序使用控制台输出的情况下,将开始时间存储在临时变量中可能会很方便:

set startTime=%time%
YourApp.exe
echo Start Time: %startTime%
echo Finish Time: %time%

如果你打开了一个命令窗口并手动调用命令,你可以在每个提示符上显示时间戳,例如:

prompt $d $t $_$P$G

它会给你这样的东西:

23.03.2009 15:45:50 77 C: \ >

如果你有一个小的批处理脚本来执行你的命令,在每个命令之前有一个空行,例如。

(空行) myCommand.exe (下一行空) myCommand2.exe

根据提示符中的时间信息,可以计算出每条命令的执行时间。最好的可能是管道输出到一个文本文件进行进一步分析:

MyBatchFile.bat > output.txt

如果有人来这里寻找这个问题的答案,有一个Windows API函数叫做GetProcessTimes()。编写一个小的C程序来启动这个命令、执行这个调用并返回进程时间,看起来并没有太多的工作。

@echo off & setlocal

set start=%time%

REM Do stuff to be timed here.
REM Alternatively, uncomment the line below to be able to
REM pass in the command to be timed when running this script.
REM cmd /c %*

set end=%time%

REM Calculate time taken in seconds, to the hundredth of a second.
REM Assumes start time and end time will be on the same day.

set options="tokens=1-4 delims=:."

for /f %options% %%a in ("%start%") do (
    set /a start_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
    set /a start_hs=100%%d %% 100
)

for /f %options% %%a in ("%end%") do (
    set /a end_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
    set /a end_hs=100%%d %% 100
)

set /a s=%end_s%-%start_s%
set /a hs=%end_hs%-%start_hs%

if %hs% lss 0 (
    set /a s=%s%-1
    set /a hs=100%hs%
)
if 1%hs% lss 100 set hs=0%hs%

echo.
echo  Time taken: %s%.%hs% secs
echo.