是否有一种内置的方法来测量Windows命令行上命令的执行时间?
当前回答
PowerShell为此提供了一个cmdlet,称为Measure-Command。您必须确保在运行PowerShell的机器上是可用的。
PS> Measure-Command { echo hi }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 1318
TotalDays : 1.52546296296296E-09
TotalHours : 3.66111111111111E-08
TotalMinutes : 2.19666666666667E-06
TotalSeconds : 0.0001318
TotalMilliseconds : 0.1318
Measure-Command捕获命令的输出。你可以使用Out-Default将输出重定向回控制台:
PS> Measure-Command { echo hi | Out-Default }
hi
Days : 0
...
正如Makotoe所评论的那样,Measure-Command返回一个TimeSpan对象,因此测量的时间被打印为一堆字段。你可以使用ToString()将对象格式化为时间戳字符串:
PS> (Measure-Command { echo hi | Out-Default }).ToString()
hi
00:00:00.0001318
如果Measure-Command中的命令改变了控制台文本的颜色,使用[console]::ResetColor()将其重置为正常。
其他回答
这是对Luke Sampson的nice timecmd.bat的评论/编辑并回复
出于某种原因,这只给我整秒的输出…这对我来说毫无用处。我的意思是,我运行timecmd pause,结果总是1秒,2秒,4秒……甚至是0.00秒!Windows 7。- Camilo Martin 13年9月25日16:00
在某些配置上,分隔符可能不同。以下变化至少应该涵盖大多数西方国家。
set options="tokens=1-4 delims=:,." (added comma)
在添加','后,%time%毫秒在我的系统上工作
(*因为网站不允许匿名评论,没有很好地跟踪身份,即使我总是使用相同的客人电子邮件,结合ipv6 ip和浏览器指纹应该足以唯一识别没有密码)
用潜水艇返回百分之一秒的时间
::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
这是一个
后置计时器版本:
使用的例子:
timeout 1 | TimeIt.cmd
Execution took ~969 milliseconds.
复制并粘贴到一些编辑器,如notepad++,并保存为TimeIt.cmd:
:: --- TimeIt.cmd ----
@echo off
setlocal enabledelayedexpansion
call :ShowHelp
:: Set pipeline initialization time
set t1=%time%
:: Wait for stdin
more
:: Set time at which stdin was ready
set t2=!time!
:: Calculate difference
Call :GetMSeconds Tms1 t1
Call :GetMSeconds Tms2 t2
set /a deltaMSecs=%Tms2%-%Tms1%
echo Execution took ~ %deltaMSecs% milliseconds.
endlocal
goto :eof
:GetMSeconds
Call :Parse TimeAsArgs %2
Call :CalcMSeconds %1 %TimeAsArgs%
goto :eof
:CalcMSeconds
set /a %1= (%2 * 3600*1000) + (%3 * 60*1000) + (%4 * 1000) + (%5)
goto :eof
:Parse
:: Mask time like " 0:23:29,12"
set %1=!%2: 0=0!
:: Replace time separators with " "
set %1=!%1::= !
set %1=!%1:.= !
set %1=!%1:,= !
:: Delete leading zero - so it'll not parsed as octal later
set %1=!%1: 0= !
goto :eof
:ShowHelp
echo %~n0 V1.0 [Dez 2015]
echo.
echo Usage: ^<Command^> ^| %~nx0
echo.
echo Wait for pipe getting ready... :)
echo (Press Ctrl+Z ^<Enter^> to Cancel)
goto :eof
^ -基于“丹尼尔·斯帕克斯”版本
我使用的是Windows XP,由于某些原因,time .exe不适合我。我找到了另一种选择——PTIME。这工作得很好。
http://www.pc-tools.net/win32/ptime/
的例子,
C:\> ptime
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
Syntax: ptime command [arguments ...]
ptime will run the specified command and measure the execution time
(run time) in seconds, accurate to 5 millisecond or better. It is an
automatic process timer, or program timer.
C:\> ptime cd
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
=== cd ===
C:\
Execution time: 0.015 s
“精益和平均”计时器与区域格式,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代表冒号结束,点结束和冒号集合,点集合-用于混合格式支持
推荐文章
- 如何计算两个时间串之间的时间间隔
- 在Windows批处理脚本中格式化日期和时间
- 映射一个网络驱动器供服务使用
- 如何在windows中使用命令提示符(cmd)列出文件。我试过在Linux中使用“ls”,但它显示一个错误?
- Windows上最好的免费c++分析器是什么?
- 如何在Windows上运行多个Python版本
- 如何精确地以毫秒为单位记录方法的执行时间?
- 运行计划任务的最佳方式
- Windows上Git文件的权限
- Ruby中DateTime和Time的区别
- 如何同时安装Python 2。3. Python。Windows下的x
- BAT文件执行后保持CMD打开
- 可能改变安卓虚拟设备保存的地方?
- 我如何在Swift中解析/创建一个以分数秒UTC时区(ISO 8601, RFC 3339)格式化的日期时间戳?
- 如何检查DLL依赖关系?