Print time in a batch file (milliseconds)

后端 未结 7 889
野性不改
野性不改 2020-12-13 23:56

How do I print the time (in ms) in a Windows batch file?

I want to measure the time that passes between lines in my batch file, but Windows\'s \"time /T\" does not p

相关标签:
7条回答
  • 2020-12-14 00:17

    If you're doing something like

    for /l %%i in (1,1,500) do @echo %time%
    

    or

    if foo (
        echo %time%
        do_something
        echo %time%
    )
    

    then you could simply put a setlocal enabledelayedexpansion at the beginning of your batch file and use !time! instead of %time% which gets evaluated on execution, not on parsing the line (which includes complete blocks enclosed in parentheses).

    0 讨论(0)
  • 2020-12-14 00:24

    To time task in CMD is as simple as

    echo %TIME% && your_command && cmd /v:on /c echo !TIME!
    
    0 讨论(0)
  • 2020-12-14 00:28

    Below batch "program" should do what you want. Please note that it outputs the data in centiseconds instead of milliseconds. The precision of the used commands is only centiseconds.

    Here is an example output:

    STARTTIME: 13:42:52,25
    ENDTIME: 13:42:56,51
    STARTTIME: 4937225 centiseconds
    ENDTIME: 4937651 centiseconds
    DURATION: 426 in centiseconds
    00:00:04,26
    

    Here is the batch script:

    @echo off
    setlocal
    
    rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
    set STARTTIME=%TIME%
    
    rem here begins the command you want to measure
    dir /s > nul
    rem here ends the command you want to measure
    
    set ENDTIME=%TIME%
    
    rem output as time
    echo STARTTIME: %STARTTIME%
    echo ENDTIME: %ENDTIME%
    
    rem convert STARTTIME and ENDTIME to centiseconds
    set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
    set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
    
    rem calculating the duratyion is easy
    set /A DURATION=%ENDTIME%-%STARTTIME%
    
    rem we might have measured the time inbetween days
    if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
    
    rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
    set /A DURATIONH=%DURATION% / 360000
    set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
    set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
    set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)
    
    rem some formatting
    if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
    if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
    if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
    if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%
    
    rem outputing
    echo STARTTIME: %STARTTIME% centiseconds
    echo ENDTIME: %ENDTIME% centiseconds
    echo DURATION: %DURATION% in centiseconds
    echo %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%
    
    endlocal
    goto :EOF
    
    0 讨论(0)
  • 2020-12-14 00:29

    %time% should work, provided enough time has elapsed between calls:

    @echo OFF
    
    @echo %time%
    ping -n 1 -w 1 127.0.0.1 1>nul
    @echo %time%
    

    On my system I get the following output:

    6:46:13.50
    6:46:13.60

    0 讨论(0)
  • 2020-12-14 00:29

    Maybe this tool (archived version ) could help? It doesn't return the time, but it is a good tool to measure the time a command takes.

    0 讨论(0)
  • 2020-12-14 00:30

    %TIME% is in the format H:MM:SS,CS after midnight and hence conversion to centiseconds >doesn't work. Seeing Patrick Cuff's post with 6:46am it seems that it is not only me.

    But with this lines bevor you should will fix that problem easy:

    if " "=="%StartZeit:~0,1%" set StartZeit=0%StartZeit:~-10%
    if " "=="%EndZeit:~0,1%" set EndZeit=0%EndZeit:~-10%
    

    Thanks for your nice inspiration! I like to use it in my mplayer, ffmpeg, sox Scripts to pimp my mediafiles for old PocketPlayers just for fun.

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