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

后端 未结 30 2977
南笙
南笙 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:19

    The following script uses only "cmd.exe" and outputs the number of milliseconds from the time a pipeline is created to the time that the process preceding the script exits. i.e., Type your command, and pipe the to the script. Example: "timeout 3 | runtime.cmd" should yield something like "2990." If you need both the runtime output and the stdin output, redirect stdin before the pipe - ex: "dir /s 1>temp.txt | runtime.cmd" would dump the output of the "dir" command to "temp.txt" and would print the runtime to the console.

    :: --- runtime.cmd ----
    @echo off
    setlocal enabledelayedexpansion
    
    :: find target for recursive calls
    if not "%1"=="" (
        shift /1
        goto :%1
        exit /b
    )
    
    :: set pipeline initialization time
    set t1=%time%
    
    :: wait for stdin
    more > nul
    
    :: set time at which stdin was ready
    set t2=!time!
    
    ::parse t1
    set t1=!t1::= !
    set t1=!t1:.= !
    set t1=!t1: 0= !
    
    :: parse t2
    set t2=!t2::= !
    set t2=!t2:.= !
    set t2=!t2: 0= !
    
    :: calc difference
    pushd %~dp0
    for /f %%i in ('%0 calc !t1!') do for /f %%j in ('%0 calc !t2!') do (
        set /a t=%%j-%%i
        echo !t!
    )
    popd
    exit /b
    goto :eof
    
    :calc
    set /a t=(%1*(3600*1000))+(%2*(60*1000))+(%3*1000)+(%4)
    echo !t!
    goto :eof
    
    endlocal
    

提交回复
热议问题