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

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

    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)

    Try copying the following script into a new batch file (e.g. 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 [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.

提交回复
热议问题