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


当前回答

我使用免费软件“GS定时器”。

只需要像这样做一个批处理文件:

timer
yourapp.exe
timer /s

如果您需要一组时间,只需将timer /s的输出输出到.txt文件中。

你可以在这里得到它:Gammadyne的免费DOS实用程序


分辨率为0.1秒。

其他回答

如果你安装了CMake,你可以运行下面的命令。

cmake -E time <the_command_to_measure_run_time>

根据您使用的Windows版本不同,只需运行bash就会进入bash模式。这将允许您使用PowerShell上不能直接使用的一系列命令(如time命令)。计时你的命令现在像执行一样简单:

# The clause <your-command> (without the angle brackets) denotes the command you want to run.
$ time <your-command>

注意:在Bash模式下运行exit可以轻松退出Bash模式并返回到主流shell。

在尝试了其他方法(如Measure-Command)后,这对我来说非常有效(Windows 10),这些方法有时会产生不需要的统计数据。希望这对你也有用。

The following script uses only "cmd.exe" and outputs the number of milliseconds from the time a pipeline is created to the time that the process preceding the script exits. i.e., Type your command, and pipe the to the script. Example: "timeout 3 | runtime.cmd" should yield something like "2990." If you need both the runtime output and the stdin output, redirect stdin before the pipe - ex: "dir /s 1>temp.txt | runtime.cmd" would dump the output of the "dir" command to "temp.txt" and would print the runtime to the console.

:: --- runtime.cmd ----
@echo off
setlocal enabledelayedexpansion

:: find target for recursive calls
if not "%1"=="" (
    shift /1
    goto :%1
    exit /b
)

:: set pipeline initialization time
set t1=%time%

:: wait for stdin
more > nul

:: set time at which stdin was ready
set t2=!time!

::parse t1
set t1=!t1::= !
set t1=!t1:.= !
set t1=!t1: 0= !

:: parse t2
set t2=!t2::= !
set t2=!t2:.= !
set t2=!t2: 0= !

:: calc difference
pushd %~dp0
for /f %%i in ('%0 calc !t1!') do for /f %%j in ('%0 calc !t2!') do (
    set /a t=%%j-%%i
    echo !t!
)
popd
exit /b
goto :eof

:calc
set /a t=(%1*(3600*1000))+(%2*(60*1000))+(%3*1000)+(%4)
echo !t!
goto :eof

endlocal

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

prompt $d $t $_$P$G

它会给你这样的东西:

23.03.2009 15:45:50 77 C: \ >

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

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

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

MyBatchFile.bat > output.txt

下面是我的方法,没有转换和没有ms。它有助于确定编码持续时间(限制在24小时内):

@echo off

:start
REM Start time storage
set ST=%time%
echo Process started at %ST%
echo.
echo.

REM Your commands
REM Your commands
REM Your commands

:end
REM Start Time Definition
for /f "tokens=1-3 delims=:" %%a in ("%ST%") do set /a h1=%%a & set /a m1=%%b & set /a s1=%%c

REM End Time Definition
for /f "tokens=1-3 delims=:" %%a in ("%TIME%") do set /a h2=%%a & set /a m2=%%b & set /a s2=%%c

REM Difference
set /a h3=%h2%-%h1% & set /a m3=%m2%-%m1% & set /a s3=%s2%-%s1%

REM Time Adjustment
if %h3% LSS 0 set /a h3=%h3%+24
if %m3% LSS 0 set /a m3=%m3%+60 & set /a h3=%h3%-1
if %s3% LSS 0 set /a s3=%s3%+60 & set /a m3=%m3%-1

echo Start    :    %ST%
echo End    :    %time%
echo.
echo Total    :    %h3%:%m3%:%s3%
echo.
pause