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

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

    my code gives you the running time in milliseconds, up to 24 hrs, it is locale insensitive, and accounts for negative values if code runs through midnight. it uses delayed expansion, and should be saved in a cmd/bat file.

    before your code:

    SETLOCAL EnableDelayedExpansion
    
    for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
    set /a t1 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t1=!t1!%t:~15,3%
    

    after your code:

    for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
    set /a t2 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t2=!t2!%t:~15,3%
    set /a t2-=t1 && if !t2! lss 0 set /a t2+=24*3600000
    

    if you want running time in HH:mm:ss.000 format, add:

    set /a "h=t2/3600000,t2%%=3600000,m=t2/60000,t2%%=60000" && set t2=00000!t2!&& set t2=!t2:~-5!
    if %h% leq 9 (set h=0%h%) && if %m% leq 9 (set m=0%m%)
    set t2=%h%:%m%:%t2:~0,2%.%t2:~2,3%
    
    ENDLOCAL
    

    variable t2 holds your running time, you can echo %t2% to display it.

提交回复
热议问题