如何在 Windows 命令行上测量命令的执行时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:10:53  来源:igfitidea点击:

How do I measure execution time of a command on the Windows command line?

windowsbatch-filetimecmdwindows-server-2003

提问by Kuroki Kaze

Is there a built-in way to measure execution time of a command on the Windows command line?

是否有内置方法可以在 Windows 命令行上测量命令的执行时间?

采纳答案by LietKynes

If you are using Windows 2003 (note that windows server 2008 and later are not supported) you can use The Windows Server 2003 Resource Kit, which contains timeit.exe that displays detailed execution stats. Here is an example, timing the command "timeit -?":

如果您使用的是 Windows 2003(请注意不支持 Windows Server 2008 及更高版本),您可以使用 Windows Server 2003 Resource Kit,其中包含显示详细执行统计信息的 timeit.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

You can get TimeIt in the Windows 2003 Resource Kit. Download it here.

您可以在 Windows 2003 Resource Kit 中获得 TimeIt。在这里下载 。

回答by Casey.K

Alternatively, Windows PowerShell has a built in command that is similar to Bash's "time" command. It is called "Measure-Command." You'll have to ensure that PowerShell is installed on the machine that runs it.

或者,Windows PowerShell 有一个类似于 Bash 的“time”命令的内置命令。它被称为“测量命令”。您必须确保 PowerShell 安装在运行它的机器上。

Example Input:

示例输入:

Measure-Command {echo hi}

Example Output:

示例输出:

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

回答by Luke Sampson

If you want

如果你想

  1. To measure execution time down to the hundredth of a second in (hh:mm:ss.ff format)
  2. To not have to download and install a resource pack
  3. To look like a huge DOS nerd (who doesn't)
  1. 以 (hh:mm:ss.ff 格式) 测量精确到百分之一秒的执行时间
  2. 不必下载和安装资源包
  3. 看起来像一个巨大的 DOS 书呆子(谁没有)

Try copying the following script into a new batch file (e.g. timecmd.bat):

尝试将以下脚本复制到新的批处理文件中(例如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)

Usage

用法

If you put timecmd.bat in a directory in your path, you can call it from anywhere like this:

如果将 timecmd.bat 放在路径中的目录中,则可以像这样从任何地方调用它:

timecmd [your command]

E.g.

例如

C:\>timecmd pause
Press any key to continue . . .
command took 0:0:1.18

If you want to do output redirection, you can quote the command like this:

如果要进行输出重定向,可以像这样引用命令:

timecmd "dir c:\windows /s > nul"

This should handle commands that run from before- to after-midnight, but the output will be wrong if your command runs for 24 hours or more.

这应该处理从午夜之前到午夜之后运行的命令,但如果您的命令运行 24 小时或更长时间,输出将是错误的。

回答by LietKynes

Hehe, the most simple solution might be this:

呵呵,最简单的解决办法可能是这样:

echo %time%
YourApp.exe
echo %time%

This works on every Windows out of the box.

这适用于开箱即用的每个 Windows。



In case of an application using console output, it might be convenient to store the starting time in a temporary variable:

对于使用控制台输出的应用程序,将开始时间存储在临时变量中可能会很方便:

set startTime=%time%
YourApp.exe
echo Start Time: %startTime%
echo Finish Time: %time%

回答by Vladimir Veljkovic

Just a little expansion of the answer from Casey.Kabout using the Measure-Commandfrom PowerShell:

Casey.K关于使用Measure-Command来自PowerShell的答案的一点扩展:

  1. You can invoke PowerShell from the standard command prompt, like this:

    powershell -Command "Measure-Command {echo hi}"
    
  2. This will eat the standard output, but you can prevent that by adding | Out-Defaultlike this from PowerShell:

    Measure-Command {echo hi | Out-Default}
    

    Or from a command prompt:

    powershell -Command "Measure-Command {echo hi | Out-Default}"
    
  1. 您可以从标准命令提示符调用 PowerShell,如下所示:

    powershell -Command "Measure-Command {echo hi}"
    
  2. 这将消耗标准输出,但您可以通过| Out-Default从 PowerShell添加如下内容来防止这种情况:

    Measure-Command {echo hi | Out-Default}
    

    或者从命令提示符:

    powershell -Command "Measure-Command {echo hi | Out-Default}"
    

Of course, you're free to wrap this in a script file *.ps1or *.bat.

当然,您可以自由地将其包装在脚本文件*.ps1*.bat.

回答by Nathan Herring

The one-liner I use in Windows Server 2008 R2 is:

我在 Windows Server 2008 R2 中使用的单行是:

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

So long as mycommanddoesn't require quotes (which screws with cmd's quote processing). The /v:onis to allow for the two different TIME values to be evaluated independently rather than once at the execution of the command.

只要mycommand不需要引号(这与 cmd 的引号处理有关)。这/v:on是为了允许在执行命令时独立评估两个不同的 TIME 值,而不是一次。

回答by Treb

If you have a command window open and call the commands manually, you can display a timestamp on each prompt, e.g.

如果您打开命令窗口并手动调用命令,则可以在每个提示上显示时间戳,例如

prompt $d $t $_$P$G

It gives you something like:

它给了你类似的东西:

23.03.2009 15:45:50,77

C:\>

23.03.2009 15:45:50,77

C:\>

If you have a small batch script that executes your commands, have an empty line before each command, e.g.

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

(empty line)

myCommand.exe

(next empty line)

myCommand2.exe

(空行)

我的命令

(下一个空行)

我的命令2.exe

You can calculate the execution time for each command by the time information in the prompt. The best would probably be to pipe the output to a textfile for further analysis:

您可以根据提示中的时间信息计算每个命令的执行时间。最好的方法可能是将输出通过管道传输到文本文件以进行进一步分析:

MyBatchFile.bat > output.txt

回答by Abe Voelker

Since others are recommending installing things like freeware and PowerShell, you could also install Cygwin, which would give you access to many basic Unix commands like time:

由于其他人建议安装免费软件和 PowerShell 之类的东西,您也可以安装Cygwin,这将使您可以访问许多基本的 Unix 命令,例如time

abe@abe-PC:~$ time sleep 5

real    0m5.012s
user    0m0.000s
sys 0m0.000s

Not sure how much overhead Cygwin adds.

不确定 Cygwin 增加了多少开销。

回答by JohnW

Not quite as elegant as some of the functionality on Unix, but create a cmd file which looks like:

不像 Unix 上的一些功能那么优雅,但创建一个 cmd 文件,如下所示:

@echo off
time < nul
yourexecutable.exe > c:\temp\output.txt
time < nul
rem on newer windows system you can try time /T

That will display the start and stop times like so:

这将显示开始和停止时间,如下所示:

The current time is: 10:31:57.92
Enter the new time:
The current time is: 10:32:05.94
Enter the new time:

回答by pepoluan

I use freeware called "GS Timer".

我使用名为“GS Timer”的免费软件。

Just make a batch file like this:

只需制作一个这样的批处理文件:

timer
yourapp.exe
timer /s

If you need a set of times, just pipe the output of timer /s into a .txt file.

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

You can get it here: Gammadyne's Free DOS Utilities

您可以在此处获取:Gammadyne 的免费 DOS 实用程序



The resolution is 0.1 seconds.

分辨率为 0.1 秒。