译(三十九)-Windows获取命令行运行时间

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

怎样获取 Windows 命令行的执行时间?

  • Kuroki Kaze asked:

    • Windows 有内置方法能获取命令行运行时间吗?
  • Answers:

    • Casey.K – vote: 571

    • PowerShell 有一个 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() 可以重置回来。

    • user87453 – vote: 331

    • 如果是为了

      1. 获取以 10ms 为步长的测量时间(用 hh:mm:ss.ff 的格式)
      2. 不下载和安装包
      3. 只想朴实无华敲几下就能实现(不会还有人不喜欢这样吧?)
    • 复制下面的脚本为 batch 文件(比如 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)
    • 用法

    • 如果把它加到系统环境(Path)下,就能像这样简单调用它:

    • timecmd [your command]
    • 例如:

    • C:\>timecmd pause
      Press any key to continue . . .
      command took 0:0:1.18
    • 如果希望将输出重定向,就像这样:

    • timecmd "dir c:\windows /s > nul"
    • 但要注意这个命令只能在半夜前用,或者半夜后用,如果运行时间超过24小时就会出现错误。

  • LietKynes – vote: 261

    • 呼呼呼,最简单的肯定是这个了:

    • echo %time%
      YourApp.exe
      echo %time%
    • 适合所有开箱即用的 Windows!

    • 译者注:Windows out of the box,我不清楚有没有其它意思,所以用了直译,即开箱即用的 Windows。


    • 考虑到应用可能会有终端输出,所以可以加上临时变量来存储开始时间:

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

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

  • Kuroki Kaze asked:

    • Is there a built-in way to measure execution time of a command on the Windows command line?
      Windows 有内置方法能获取命令行运行时间吗?
  • Answers:

    • Casey.K – vote: 571

    • PowerShell has a cmdlet for this called Measure-Command. You\’ll have to ensure that PowerShell is available on the machine that runs it.
      PowerShell 有一个 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 captures the command\’s output. You can redirect the output back to your console using Out-Default:
      Measure-Command 会捕获命令的输出,但可以用 Out-Default 来将输出重定向至终端里。

    • PS> Measure-Command { echo hi | Out-Default }
      hi
      
      Days              : 0
      ...
    • As Makotoe commented, Measure-Command returns a TimeSpan object, so the measured time is printed as a bunch of fields. You can format the object into a timestamp string using ToString():
      正如 Makotoe 提到的, Measure-Command 返回一个 TimeSpan 对象,所以被测量的时间会以字段形式打印出来。使用 ToString() 可以将这个对象转为时间戳字符串。

    • PS> (Measure-Command { echo hi | Out-Default }).ToString()
      hi
      00:00:00.0001318
    • If the command inside Measure-Command changes your console text color, use [Console]::ResetColor() to reset it back to normal.
      如果 Measure-Command 改变了终端文本颜色的话,[Console]::ResetColor() 可以重置回来。

    • user87453 – vote: 331

    • If you want
      如果是为了

      1. To measure execution time down to the hundredth of a second in (hh:mm:ss.ff format)
        获取以 10ms 为步长的测量时间(用 hh:mm:ss.ff 的格式)
      2. To not have to download and install a resource pack
        不下载和安装包
      3. To look like a huge DOS nerd (who doesn\’t)
        只想朴实无华敲几下就能实现(不会还有人不喜欢这样吧?)
    • Try copying the following script into a new batch file (e.g. timecmd.bat):
      复制下面的脚本为 batch 文件(比如 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:
      如果把它加到系统环境(Path)下,就能像这样简单调用它:

    • 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小时就会出现错误。

  • LietKynes – vote: 261

    • Hehe, the most simple solution might be this:
      呼呼呼,最简单的肯定是这个了:

    • echo %time%
      YourApp.exe
      echo %time%
    • This works on every Windows out of the box.
      适合所有开箱即用的 Windows!

    • 译者注: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%

You may also like...

发表评论

您的电子邮箱地址不会被公开。

9 + 1 =