是否有一种内置的方法来测量Windows命令行上命令的执行时间?
当前回答
如果有人来这里寻找这个问题的答案,有一个Windows API函数叫做GetProcessTimes()。编写一个小的C程序来启动这个命令、执行这个调用并返回进程时间,看起来并没有太多的工作。
其他回答
我使用的是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小时…
@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.
这是一个
后置计时器版本:
使用的例子:
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
^ -基于“丹尼尔·斯帕克斯”版本
这是一个单行程序,可以避免延迟展开,这可能会扰乱某些命令:
cmd /E /C "prompt $T$$ & echo.%TIME%$ & COMMAND_TO_MEASURE & for %Z in (.) do rem/ "
输出如下所示:
14:30:27.58元 ... 14:32:43.17雷姆/元
对于长期测试,将$T替换为$D、$T和%TIME%替换为%DATE%、%TIME%以包含日期。
要在批处理文件中使用,请将%%Z替换为%%Z。
更新
下面是一个改进的单行程序(也没有延迟展开):
cmd /E /C "prompt $D, $T$$ & (for %# in (.) do rem/ ) & COMMAND_TO_MEASURE & for %# in (.) do prompt"
输出如下所示:
2015/09/01, 14:30:27.58$ rem/ ... 2015/09/01, 14:32:43.17$ 提示
这种方法不包括在结果中实例化一个新的cmd的过程,也不包括提示命令。
如果您使用的是Windows 2003(注意不支持Windows server 2008及更高版本),您可以使用Windows server 2003资源工具包,其中包含显示详细执行统计信息的time .exe。这里有一个例子,计时命令“timeit -?”:
C:\>timeit timeit -?
Invalid switch -?
Usage: TIMEIT [-f filename] [-a] [-c] [-i] [-d] [-s] [-t] [-k keyname | -r keyname] [-m mask] [commandline...]
where: -f specifies the name of the database file where TIMEIT
keeps a history of previous timings. Default is .\timeit.dat
-k specifies the keyname to use for this timing run
-r specifies the keyname to remove from the database. If
keyname is followed by a comma and a number then it will
remove the slowest (positive number) or fastest (negative)
times for that keyname.
-a specifies that timeit should display average of all timings
for the specified key.
-i specifies to ignore non-zero return codes from program
-d specifies to show detail for average
-s specifies to suppress system wide counters
-t specifies to tabular output
-c specifies to force a resort of the data base
-m specifies the processor affinity mask
Version Number: Windows NT 5.2 (Build 3790)
Exit Time: 7:38 am, Wednesday, April 15 2009
Elapsed Time: 0:00:00.000
Process Time: 0:00:00.015
System Calls: 731
Context Switches: 299
Page Faults: 515
Bytes Read: 0
Bytes Written: 0
Bytes Other: 298
您可以在Windows 2003资源工具包中获得TimeIt。它不能从微软下载中心直接下载,但仍然可以从archive.org - Windows Server 2003资源工具包工具中获得。
推荐文章
- 如何计算两个时间串之间的时间间隔
- 在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依赖关系?