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

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

    The answer of driblio can be made a little shorter (though not much readable)

    @echo off
    
    :: Calculate the start timestamp
    set _time=%time%
    set /a _hours=100%_time:~0,2%%%100,_min=100%_time:~3,2%%%100,_sec=100%_time:~6,2%%%100,_cs=%_time:~9,2%
    set /a _started=_hours*60*60*100+_min*60*100+_sec*100+_cs
    
    
    :: yourCommandHere
    
    
    :: Calculate the difference in cSeconds
    set _time=%time%
    set /a _hours=100%_time:~0,2%%%100,_min=100%_time:~3,2%%%100,_sec=100%_time:~6,2%%%100,_cs=%_time:~9,2%
    set /a _duration=_hours*60*60*100+_min*60*100+_sec*100+_cs-_started
    
    :: Populate variables for rendering (100+ needed for padding)
    set /a _hours=_duration/60/60/100,_min=100+_duration/60/100%%60,_sec=100+(_duration/100%%60%%60),_cs=100+_duration%%100
    
    echo Done at: %_time% took : %_hours%:%_min:~-2%:%_sec:~-2%.%_cs:~-2%
    
    ::prints something like:
    ::Done at: 12:37:53,70 took: 0:02:03.55
    

    To the remark of Luke Sampson this version is octal safe, though the task should be completed in 24 hours.

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

    Here is my method, no conversion and no ms. It is useful to determine encoding durations (limited to 24 hours though):

    @echo off
    
    :start
    REM Start time storage
    set ST=%time%
    echo Process started at %ST%
    echo.
    echo.
    
    REM Your commands
    REM Your commands
    REM Your commands
    
    :end
    REM Start Time Definition
    for /f "tokens=1-3 delims=:" %%a in ("%ST%") do set /a h1=%%a & set /a m1=%%b & set /a s1=%%c
    
    REM End Time Definition
    for /f "tokens=1-3 delims=:" %%a in ("%TIME%") do set /a h2=%%a & set /a m2=%%b & set /a s2=%%c
    
    REM Difference
    set /a h3=%h2%-%h1% & set /a m3=%m2%-%m1% & set /a s3=%s2%-%s1%
    
    REM Time Adjustment
    if %h3% LSS 0 set /a h3=%h3%+24
    if %m3% LSS 0 set /a m3=%m3%+60 & set /a h3=%h3%-1
    if %s3% LSS 0 set /a s3=%s3%+60 & set /a m3=%m3%-1
    
    echo Start    :    %ST%
    echo End    :    %time%
    echo.
    echo Total    :    %h3%:%m3%:%s3%
    echo.
    pause
    
    0 讨论(0)
  • 2020-11-22 10:06

    As long as it doesn't last longer than 24hours...

    @echo off
    
    set starttime=%TIME%
    set startcsec=%STARTTIME:~9,2%
    set startsecs=%STARTTIME:~6,2%
    set startmins=%STARTTIME:~3,2%
    set starthour=%STARTTIME:~0,2%
    set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)
    
    :TimeThis
    ping localhost 
    
    set endtime=%time%
    set endcsec=%endTIME:~9,2%
    set endsecs=%endTIME:~6,2%
    set endmins=%endTIME:~3,2%
    set endhour=%endTIME:~0,2%
    if %endhour% LSS %starthour% set /a endhour+=24
    set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)
    
    set /a timetaken= ( %endtime% - %starttime% )
    set /a timetakens= %timetaken% / 100
    set timetaken=%timetakens%.%timetaken:~-2%
    
    echo.
    echo Took: %timetaken% sec.
    
    0 讨论(0)
  • 2020-11-22 10:07

    This is a comment/edit to Luke Sampson's nice timecmd.bat and reply to

    For some reason this only gives me output in whole seconds... which for me is useless. I mean that I run timecmd pause, and it always results in 1.00 sec, 2.00 sec, 4.00 sec... even 0.00 sec! Windows 7. – Camilo Martin Sep 25 '13 at 16:00 "

    On some configurations the delimiters may differ. The following change should cover atleast most western countries.

    set options="tokens=1-4 delims=:,." (added comma)
    

    The %time% milliseconds work on my system after adding that ','

    (*because site doesn't allow anon comment and doesn't keep good track of identity even though I always use same guest email which combined with ipv6 ip and browser fingerprint should be enough to uniquely identify without password)

    0 讨论(0)
  • 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.

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

    Here is a

    Postfix timer version:

    Usage example:

    timeout 1 | TimeIt.cmd

    Execution took  ~969 milliseconds.
    

    Copy & paste this into some editor like for example Notepad++ and save it as TimeIt.cmd:

    :: --- TimeIt.cmd ----
        @echo off
        setlocal enabledelayedexpansion
    
        call :ShowHelp
    
        :: Set pipeline initialization time
        set t1=%time%
    
        :: Wait for stdin
        more
    
        :: Set time at which stdin was ready
        set t2=!time!
    
    
        :: Calculate difference
        Call :GetMSeconds Tms1 t1
        Call :GetMSeconds Tms2 t2
    
        set /a deltaMSecs=%Tms2%-%Tms1%
        echo Execution took ~ %deltaMSecs% milliseconds.
    
        endlocal
    goto :eof
    
    :GetMSeconds
        Call :Parse        TimeAsArgs %2
        Call :CalcMSeconds %1 %TimeAsArgs%
    
    goto :eof
    
    :CalcMSeconds
        set /a %1= (%2 * 3600*1000) + (%3 * 60*1000) + (%4 * 1000) + (%5)
    goto :eof
    
    :Parse
    
        :: Mask time like " 0:23:29,12"
        set %1=!%2: 0=0!
    
        :: Replace time separators with " "
        set %1=!%1::= !
        set %1=!%1:.= !
        set %1=!%1:,= !
    
        :: Delete leading zero - so it'll not parsed as octal later
        set %1=!%1: 0= !
    goto :eof
    
    :ShowHelp
        echo %~n0 V1.0 [Dez 2015]
        echo.
        echo Usage: ^<Command^> ^| %~nx0
        echo.
        echo Wait for pipe getting ready... :)
        echo  (Press Ctrl+Z ^<Enter^> to Cancel)
    goto :eof
    

    ^ - Based on 'Daniel Sparks' Version

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