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


当前回答

“精益和平均”计时器与区域格式,24小时和混合输入支持 调整Aacini的替换方法体,没有IF,只有一个FOR(我的区域修复)

1:文件timer.bat放置在%PATH%或当前目录的某处

@echo off & rem :AveYo: compact timer function with Regional format, 24-hours and mixed input support
if not defined timer_set (if not "%~1"=="" (call set "timer_set=%~1") else set "timer_set=%TIME: =0%") & goto :eof
(if not "%~1"=="" (call set "timer_end=%~1") else set "timer_end=%TIME: =0%") & setlocal EnableDelayedExpansion
for /f "tokens=1-6 delims=0123456789" %%i in ("%timer_end%%timer_set%") do (set CE=%%i&set DE=%%k&set CS=%%l&set DS=%%n)
set "TE=!timer_end:%DE%=%%100)*100+1!"     & set "TS=!timer_set:%DS%=%%100)*100+1!"
set/A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set/A "T=!T:-=8640000-!"
set/A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
endlocal & set "timer_end=%value%" & set "timer_set=" & goto :eof

用法: Timer & echo start_cmds & timeout /t 3 & echo end_cmds & Timer 计时器&计时器“23:23:23,00” 计时器“23:23:23,00”&计时器 计时器“13.23.23,00”&计时器“03:03:03.00” 定时器&定时器“0:00:00.00”no & CMD /v:on /c echo until midnight=!timer_end! 对于执行过程中不太可能但可能发生的时间格式更改,现在可以混合输入

2:函数:与批处理脚本绑定的定时器(示例用法如下):

@echo off
set "TIMER=call :timer" & rem short macro
echo.
echo EXAMPLE:
call :timer
timeout /t 3 >nul & rem Any process here..
call :timer
echo.
echo SHORT MACRO:
%TIMER% & timeout /t 1 & %TIMER% 
echo.
echo TEST INPUT:
set "start=22:04:04.58"
set "end=04.22.44,22"
echo %start% ~ start & echo %end% ~ end
call :timer "%start%"
call :timer "%end%"
echo.
%TIMER% & %TIMER% "00:00:00.00" no 
echo UNTIL MIDNIGHT: %timer_end%
echo.
pause 
exit /b

::要测试它,复制粘贴上面和下面的代码段

rem :AveYo: compact timer function with Regional format, 24-hours and mixed input support 
:timer Usage " call :timer [input - optional] [no - optional]" :i Result printed on second call, saved to timer_end 
if not defined timer_set (if not "%~1"=="" (call set "timer_set=%~1") else set "timer_set=%TIME: =0%") & goto :eof
(if not "%~1"=="" (call set "timer_end=%~1") else set "timer_end=%TIME: =0%") & setlocal EnableDelayedExpansion
for /f "tokens=1-6 delims=0123456789" %%i in ("%timer_end%%timer_set%") do (set CE=%%i&set DE=%%k&set CS=%%l&set DS=%%n)
set "TE=!timer_end:%DE%=%%100)*100+1!"     & set "TS=!timer_set:%DS%=%%100)*100+1!"
set/A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set/A "T=!T:-=8640000-!"
set/A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
endlocal & set "timer_end=%value%" & set "timer_set=" & goto :eof

CE,DE和CS,DS代表冒号结束,点结束和冒号集合,点集合-用于混合格式支持

其他回答

只要不超过24小时…

@echo off

set starttime=%TIME%
set startcsec=%STARTTIME:~9,2%
set startsecs=%STARTTIME:~6,2%
set startmins=%STARTTIME:~3,2%
set starthour=%STARTTIME:~0,2%
set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)

:TimeThis
ping localhost 

set endtime=%time%
set endcsec=%endTIME:~9,2%
set endsecs=%endTIME:~6,2%
set endmins=%endTIME:~3,2%
set endhour=%endTIME:~0,2%
if %endhour% LSS %starthour% set /a endhour+=24
set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)

set /a timetaken= ( %endtime% - %starttime% )
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%

echo.
echo Took: %timetaken% sec.

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

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

timer
yourapp.exe
timer /s

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

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


分辨率为0.1秒。

我在Windows Server 2008 R2中使用的一行程序是:

cmd /v:on /c "echo !TIME! & *mycommand* & echo !TIME!"

只要mycommand不需要引号(这会影响cmd的引号处理)。on允许两个不同的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),这些方法有时会产生不需要的统计数据。希望这对你也有用。

在Perl安装了可用的雇佣解决方案后,运行:

C:\BATCH>time.pl "echo Fine result"
0.01063
Fine result

STDERR出现在被测量的秒之前

#!/usr/bin/perl -w

use Time::HiRes qw();
my $T0 = [ Time::HiRes::gettimeofday ];

my $stdout = `@ARGV`;

my $time_elapsed = Time::HiRes::tv_interval( $T0 );

print $time_elapsed, "\n";
print $stdout;