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

后端 未结 30 2944
南笙
南笙 2020-11-22 09:44

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

相关标签:
30条回答
  • 2020-11-22 10:10

    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:\>

    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

    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
    
    0 讨论(0)
  • 2020-11-22 10:12

    Using a sub to return time in hundredths of second

    ::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
    
    0 讨论(0)
  • 2020-11-22 10:14

    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.

    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
    
    0 讨论(0)
  • 2020-11-22 10:15

    Another approach with powershell:

    @echo off
    for /f %%t in ('powershell "(get-date).tofiletime()"') do set mst=%%t
    
    rem some commands
    
    powershell ((get-date).tofiletime() - %mst%)
    

    this will print the execution time in milliseconds.

    0 讨论(0)
  • 2020-11-22 10:17

    Just a little expansion of the answer from Casey.K about using the Measure-Command from 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-Default like this from PowerShell:

      Measure-Command {echo hi | Out-Default}
      

      Or from a command prompt:

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

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

    0 讨论(0)
  • 2020-11-22 10:17

    There's also TimeMem (March 2012):

    This is a Windows utility which executes a program and displays its execution time, memory usage, and IO statistics. It is similar in functionality to the Unix time utility.

    0 讨论(0)
提交回复
热议问题