Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Calculating using time  (Read 9096 times)

0 Members and 1 Guest are viewing this topic.

Frank

    Topic Starter


    Intermediate

    Thanked: 3
    Calculating using time
    « on: August 11, 2008, 03:27:05 AM »
    I also have a calculating problem.
    I would like to calculate how long some commands take.
    For example:

    @echo off
    set old=%time%
    echo %old%
    rem some commands here
    echo %old% %time%
    set /A delay="%time% - %old%"
    echo %delay%
    pause

    I get an error "Missing operator" with an 18 on the following line.
    If I remove the % signs I don't get an error just "-19" on the following line. There must be some sort of a syntax error, but I don't know enough about the set command it to rectify it. It would be nice if I understood the help file (set /?).
    Any help would be appreciated.
    Frank

    Dias de verano

    • Guest
    Re: Calculating using time
    « Reply #1 on: August 11, 2008, 03:42:58 AM »
    what does %time% look like?

    Frank

      Topic Starter


      Intermediate

      Thanked: 3
      Re: Calculating using time
      « Reply #2 on: August 11, 2008, 03:45:58 AM »
      %time% looks like 19:44:16.85
      Frank

      Dias de verano

      • Guest
      Re: Calculating using time
      « Reply #3 on: August 11, 2008, 04:25:53 AM »
      You have to remove the colons, separate out the hours, minutes and seconds, multiply hours x 3600, minutes x 60, and add to seconds to get seconds since midnight, then you can subtract one from another.

      Frank

        Topic Starter


        Intermediate

        Thanked: 3
        Re: Calculating using time
        « Reply #4 on: August 11, 2008, 04:28:55 AM »
        o/k, this no longer sounds like a job for a batch file.
        Frank

        Dias de verano

        • Guest
        Re: Calculating using time
        « Reply #5 on: August 11, 2008, 04:34:54 AM »
        can be done, I've done it, when I get home in about 6 hrs I'll post it. The big trouble comes when you span midnight. You can get the PC's up time from Windows and use that. Catch me later.

        Frank

          Topic Starter


          Intermediate

          Thanked: 3
          Re: Calculating using time
          « Reply #6 on: August 11, 2008, 04:38:39 AM »
          Thank you very much.
          In 6.5 hours it will be 3:00am here so I will be asleep. But I'll look at the post when I get up.
          Thanks again.
          Frank

          Dias de verano

          • Guest
          Re: Calculating using time
          « Reply #7 on: August 11, 2008, 11:07:55 AM »
          Have you got XP pro?

          can you use the systeminfo command?


          Frank

            Topic Starter


            Intermediate

            Thanked: 3
            Re: Calculating using time
            « Reply #8 on: August 11, 2008, 03:33:59 PM »
            Yes, I have Windows XP Pro SP2.

            Dias de verano

            • Guest
            Re: Calculating using time
            « Reply #9 on: August 11, 2008, 03:45:03 PM »
            Number of seconds since computer was last booted:

            for /f "tokens=1-14 delims= " %%A in ('systeminfo ^| find "System Up Time"') do set /a secs=86400*%%D+3600*%%F+60*%%H+%%J

            Frank

              Topic Starter


              Intermediate

              Thanked: 3
              Re: Calculating using time
              « Reply #10 on: August 11, 2008, 11:27:54 PM »
              Thank you very much.
              This is what I did:

              @echo off
              for /f "tokens=1-14 delims= " %%A in ('systeminfo ^| find "System Up Time"') do set /a secs1=86400*%%D+3600*%%F+60*%%H+%%J
              rem some commands to do
              for /f "tokens=1-14 delims= " %%A in ('systeminfo ^| find "System Up Time"') do set /a secs2=86400*%%D+3600*%%F+60*%%H+%%J
              set /a secs3=secs2-secs1
              echo %secs3% seconds taken.
              pause

              I am not sure I understand the syntax, expecially %%H etc. but it works that's the main thing.
              Thanks

              Dias de verano

              • Guest
              Re: Calculating using time
              « Reply #11 on: August 12, 2008, 01:57:00 AM »
              regarding the syntax - the output of systeminfo is a long string of words separated by spaces, so %%B to %% J are "implicit variables" created by the tokens=1-14 section. Actually, 1-11 would do just as well.

              You should note that the actual systeminfo operation takes about 2 seconds, (on my system) so you should subtract that from the second time. You can establish the size of this delay overhead by running the code like this


              Code: [Select]
              @echo off
              for /f "tokens=1-14 delims= " %%A in ('systeminfo ^| find "System Up Time"') do set /a secs1=86400*%%D+3600*%%F+60*%%H+%%J

              rem nothing here

              for /f "tokens=1-14 delims= " %%A in ('systeminfo ^| find "System Up Time"') do set /a secs2=86400*%%D+3600*%%F+60*%%H+%%J
              set /a secs3=secs2-secs1
              echo %secs3% seconds taken.
              pause

              Frank

                Topic Starter


                Intermediate

                Thanked: 3
                Re: Calculating using time
                « Reply #12 on: August 12, 2008, 04:06:35 PM »
                Thanks,
                Tried it and the overhead on my system is also 2 seconds.

                Dias de verano

                • Guest
                Re: Calculating using time
                « Reply #13 on: August 13, 2008, 12:46:36 PM »
                If you do not mind that this will give wrong results if the first time is before midnight, and the second time is after (the next, or a subsequent!) midnight, then this code takes the hours, minutes, seconds, and hundredths of a second in the %time% variable and converts to a figure in milliseconds, and it takes much less than 2 seconds! I would not trust it to beyond around 50 msec though.

                Code: [Select]
                @echo off
                set ntime=%time%

                REM slice up %time%
                REM presumes 24 hr clock hh:mm:ss.nn
                REM nn=1/100ths of seconds
                set hh=%ntime:~0,2%
                set mm=%ntime:~3,2%
                set ss=%ntime:~6,2%
                set cs=%ntime:~9,2%

                REM remove any leading zeroes because set /a
                REM treats them as indicating octal values
                REM and barfs at 08 and 09, understandably!
                REM also remove leading space if hour < 10
                if "%hh:~0,1%"==" " set hh=%hh:~1,1%
                if "%mm:~0,1%"=="0" set mm=%mm:~1,1%
                if "%ss:~0,1%"=="0" set ss=%ss:~1,1%
                if "%cs:~0,1%"=="0" set cs=%cs:~1,1%

                REM now they are all valid integer values, do the arithmetic
                set /a msec=3600000*%hh%+60000*%mm%+1000*%ss%+10*%cs%
                echo %msec%

                say you called it mtime.bat and stored it somewhere on your PATH, you could use it like this

                With just that "REM code to be timed" line I am mostly seeing values between 100 & 200 msec with a few as low as 70 or as high as 230.

                Code: [Select]
                @echo off
                for /f %%T in ('mtime.bat') do set time1=%%T
                REM code to be timed
                for /f %%T in ('mtime.bat') do set time2=%%T
                set /a elapsed=%time2%-%time1%
                echo elapsed %elapsed% milliseconds

                Frank

                  Topic Starter


                  Intermediate

                  Thanked: 3
                  Re: Calculating using time
                  « Reply #14 on: August 14, 2008, 02:53:02 AM »
                  That is cool.
                  Overhead on my computer is 110-140ms.
                  This is a much better solution that stepping through a file.
                  Thanks

                  Just thought, a dummy run could be made to determine the overhead and then subtract that amount at the end. This would be more acurate. The error that still exists is the variation between successive runs (+- 20ms).

                  Code: [Select]
                  @echo off
                  rem DO A DUMMY RUN TO FIND THE OVERHEAD
                  for /f %%T in ('mtime.bat') do set time1=%%T
                  for /f %%T in ('mtime.bat') do set time2=%%T
                  set /a overh=%time2%-%time1%

                  for /f %%T in ('mtime.bat') do set time1=%%T
                  REM code to be timed
                  for /f %%T in ('mtime.bat') do set time2=%%T
                  set /a elapsed=%time2%-%time1%-%overh%
                  echo elapsed %elapsed% milliseconds
                  pause
                  « Last Edit: August 14, 2008, 03:44:24 AM by Frank »