是否有一种内置的方法来测量Windows命令行上命令的执行时间?
当前回答
根据您使用的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),这些方法有时会产生不需要的统计数据。希望这对你也有用。
其他回答
我在Windows Server 2008 R2中使用的一行程序是:
cmd /v:on /c "echo !TIME! & *mycommand* & echo !TIME!"
只要mycommand不需要引号(这会影响cmd的引号处理)。on允许两个不同的TIME值独立计算,而不是在执行命令时一次计算。
在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;
还有TimeMem(2012年3月):
这是一个Windows实用程序,执行一个程序并显示它 执行时间、内存使用和IO统计信息。这与 Unix time实用程序的功能。
如果你愿意
测量执行时间精确到(hh:mm:ss)的百分之一秒。ff格式) 不需要下载和安装资源包 看起来像一个巨大的DOS书呆子(谁不是呢)
尝试将以下脚本复制到一个新的批处理文件(例如timecmd.bat):
@echo off
@setlocal
set start=%time%
:: Runs your command
cmd /c %*
set end=%time%
set options="tokens=1-4 delims=:.,"
for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100
set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %hours% lss 0 set /a hours = 24%hours%
if 1%ms% lss 100 set ms=0%ms%
:: Mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%
echo command took %hours%:%mins%:%secs%.%ms% (%totalsecs%.%ms%s total)
使用
如果你把timecmd.bat放在你路径中的一个目录中,你可以在任何地方调用它,像这样:
timecmd [your command]
E.g.
C:\>timecmd pause
Press any key to continue . . .
command took 0:0:1.18
如果你想做输出重定向,你可以这样引用命令:
timecmd "dir c:\windows /s > nul"
这应该可以处理从午夜之前到午夜之后运行的命令,但是如果您的命令运行了24小时或更长时间,则输出将是错误的。
只要不超过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.
推荐文章
- 如何从gmtime()的时间+日期输出中获得自epoch以来的秒数?
- 如何在批处理文件中请求管理员访问
- 有没有替换Windows (Visual C)的unistd.h ?
- 从Windows批处理文件设置系统环境变量?
- 当组件属性依赖于当前日期时间时,如何管理Angular2“expression has changed after it was checked”异常
- 在批处理文件中处理子字符串的最佳方法是什么?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 如何添加一个设置路径只执行批处理文件?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- BAT文件:打开新的cmd窗口并在其中执行命令