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

Author Topic: Using SET and time in batch file, want to get the difference in time  (Read 34024 times)

0 Members and 1 Guest are viewing this topic.

tlmercur

    Topic Starter


    Newbie

    Hi,

    I want to find out how long a process took by accessing either time or some other object at the beginning of the process, then again at the end and compare them to see how long it took.

    The process to be measure takes less than 20 minutes, units of measurement can be time or just some unit that is consistent.

    Here is example:
    set time1=%time%
    some process runs for a while
    set time2=%time%
    set /a timetotal=(time2-time1)

    of course in this example timetotal will equal 0.

    Thanks.

    Dias de verano

    • Guest
    Re: Using SET and time in batch file, want to get the difference in time
    « Reply #1 on: March 03, 2009, 01:00:11 AM »
    please state what the result of time /t is on your system. (I.e. your local time format)

    billrich

    • Guest
    Re: Using SET and time in batch file, want to get the difference in time
    « Reply #2 on: March 03, 2009, 06:14:02 PM »
    Timercur,


    C:\>timeinseconds.bat
    start time=13:24:37.71
    Hour=13
    minutes=24
    Total Seconds in given minutes=1440
    Total seconds in given  Hours = 46800
    seconds= 37
    start time in seconds =48277
    end time=13:24:47.97
    End Hour=13
    minutes=24
    Total Seconds in given minutes=1440
    Total seconds in given  Hours = 46800
    seconds= 47
    end time in seconds =48287
    Total Time used in seconds = 10
    C:\>type timeinseconds.bat
    @echo OFF
    rem start - end  time

    REM time format 12:46:26.94

    echo start time=%TIME%

    set HH=%TIME:~0,2%

    echo Hour=%HH%

    set MM=%TIME:~3,2%

    echo minutes=%MM%

    set /a MM=%MM% * 60

    echo Total Seconds in given minutes=%MM%


    set /a HH=%HH% * 3600

    echo Total seconds in given  Hours = %HH%

    set SS=%TIME:~6,2%

    echo seconds= %SS%


    Set /a start=%HH% + %MM% + %SS%

    echo start time in seconds =%start%

    echo enter time to sleep

    set /p  sleep=

    sleep %sleep%

    Rem end time in seconds

    echo end time=%TIME%

    set HH=%TIME:~0,2%

    echo End Hour=%HH%

    set MM=%TIME:~3,2%
    echo minutes=%MM%

    set /a MM=%MM% * 60

    echo Total Seconds in given minutes=%MM%


    set /a HH=%HH% * 3600

    echo Total seconds in given  Hours = %HH%

    set SS=%TIME:~6,2%

    echo seconds= %SS%


    Set /a end=%HH% + %MM% + %SS%

    echo end time in seconds =%end%

    set /a totaltime=%end% - %start%

    echo Total Time used in seconds = %totaltime%


    C:\>
    « Last Edit: March 05, 2009, 12:55:58 PM by billrich »

    tlmercur

      Topic Starter


      Newbie

      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #3 on: March 03, 2009, 10:18:13 PM »
      In response to Dias de verano, ==> time /t  ==> 09:08 PM

      billrich - thanks for the response.

      I tried striptime.bat as is and noted it does skew after ~30 seconds.  (30 s came out to be 73.)
      It did give me some additional insight to what I might be able to do with %str::=% which I did not know could be done.

      Thanks!


      Dias de verano

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #4 on: March 04, 2009, 12:17:57 AM »
      Quote from: Billrich
      if time2 is 20 Hundred Hours: 32 minutes: 17 seconds and time1 =  19 Hundred hours and 59 minutes and 17 seconds. The integer for time2 = 203217 and the integer for time1 = 195917.

      That's a weird way of doing it, and wrong as well. Why not multiply the hours by 3600, [add 43200 if it is PM] the minutes by 60 and add 'em to the seconds?




      billrich

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #5 on: March 04, 2009, 07:56:41 AM »
      Dias de verano wrote>
      "That's a weird way of doing it, and wrong as well. Why not multiply the hours by 3600, [add 43200 if it is PM] the minutes by 60 and add 'em to the seconds? "

      http://www.verge-rpg.com/boards/display_thread.php?id=28603

      The maximum integer size is 2 ^ 31 - 1 = 2,147,483,647.This is because it is a signed 32-bit integer, ie. 32 bit but it uses one bit to store if the number is negative or positive. If you overflow this limit, you'll end up with unexpected results that are likely in the negatives. The minimum limit for integer values is -2 ^ 31 = -2,147,483,648. I don't know of any easy way to work around this limit, but I'm sure it'd involve something like creating your own number storage system. :/

      Is there another approach for the difference in time?  The integer approach seems to be limited.  DOS does not use floating point? Can we instruct the computer to do the arithmetic like we do on paper?  When we borrow from hours we borrow 60 minutes? The internet might have an example?

      The integer in seconds might work?
         (3600 * 24) - (12 * 3600) = 43200
         

      Dias de verano

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #6 on: March 04, 2009, 08:42:36 AM »
      The maximum integer size is 2 ^ 31 - 1 = 2,147,483,647.This is because it is a signed 32-bit integer, ie. 32 bit but it uses one bit to store if the number is negative or positive. If you overflow this limit, you'll end up with unexpected results that are likely in the negatives. The minimum limit for integer values is -2 ^ 31 = -2,147,483,648. I don't know of any easy way to work around this limit, but I'm sure it'd involve something like creating your own number storage system. :/

      Is there another approach for the difference in time?  The integer approach seems to be limited.  DOS does not use floating point? Can we instruct the computer to do the arithmetic like we do on paper?  When we borrow from hours we borrow 60 minutes? The internet might have an example?

      The integer in seconds might work?
         (3600 * 24) - (12 * 3600) = 43200
         

      Huh? What are you talking about? There are not (2^31)-1 seconds in a day. The maximum integer seconds value you can get from the system clock is going to be 23 hrs 59 mins 59 seconds which is 86,399 seconds. Anyway, 2^31 seconds is about 68 years, so you could time a long task with a resolution of 1 second.

      For serious counting and timing you would not use batch. You would use VBScript or some other language where you can have bigger integers than 32 bit.




      billrich

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #7 on: March 04, 2009, 09:11:08 AM »
      Dias de verano wrote:
      <<"For serious counting and timing you would not use batch. You would use VBScript or some other language where you can have bigger integers than 32 bit.">>


      Ok, so show the Batch code for  Timecur, the original poster with the question  about difference in time?






      Dias de verano

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #8 on: March 04, 2009, 09:12:54 AM »
      I did already about 6 months ago. Anyway, you seem to be the expert, I don't want to get in your way.

      « Last Edit: March 04, 2009, 09:50:51 AM by Dias de verano »

      billrich

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #9 on: March 04, 2009, 09:51:54 AM »
      Dias de verano


      "I did already about 6 months ago."


      I was not here six months ago. I did a search of the archives but could not find an old thread using "Using SET and time in batch file, I want to get the difference in time." 

      Would you provide a link to the Thread for us new guys?

      Thanks for your help.  The old code does not help if no one sees it.

      Dias de verano

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #10 on: March 04, 2009, 09:57:47 AM »
      You're right! I tried searching and could not find it. I know I could find it by going back through my posts but there are 280 pages of them!

      Anyway, this will work as long as

      (1) the task to be timed takes less than 24 hours
      (2) The task does not start before midnight and end after midnight.

      Code: [Select]
      @echo off
      echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(timer)"') do set start=%%A

      REM TASK TO BE TIMED HERE
      REM Example:
      ping www.google.com

      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(timer)"') do set end=%%A
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(%end%-%start%)"') do set longnumber=%%A
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "FormatNumber(%longnumber%,2,-1)"') do set elapsed=%%A
      echo times in seconds
      echo start   %start%
      echo end     %end%
      echo elapsed %elapsed% second(s)

      Dias de verano

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #11 on: March 04, 2009, 10:26:48 AM »
      Another thing you can do is get the Windows Uptime Tool which is a free download from Microsoft

      http://support.microsoft.com/kb/232243

      This shows the number of days hours minutes and seconds since Windows was booted, which gets around the midnight problem. However the task must not take longer than 68 years.

      Code: [Select]
      @echo off
      for /f "tokens=1-12 delims=,: " %%A in ('uptime.exe') do (
           set UpDays=%%F
           set UpHours=%%H
           set Upminutes=%%J
           set UpSeconds=%%L
           )
      set /a seconds=86400*%UpDays%
      set /a seconds=%seconds%+(3600*%UpHours%)
      set /a seconds=%seconds%+(60*%UpMinutes%)
      set /a seconds=%seconds%+%UpSeconds%
      set /a start=%seconds%

      REM task(s) to be timed here

      ping www.google.com
      ping www.bbc.co.uk

      for /f "tokens=1-12 delims=,: " %%A in ('uptime.exe') do (
           set UpDays=%%F
           set UpHours=%%H
           set Upminutes=%%J
           set UpSeconds=%%L
           )
      set /a seconds=86400*%UpDays%
      set /a seconds=%seconds%+(3600*%UpHours%)
      set /a seconds=%seconds%+(60*%UpMinutes%)
      set /a seconds=%seconds%+%UpSeconds%
      set /a finish=%seconds%

      set /a elapsed=%finish%-%start%

      echo Elapsed: %elapsed% second(s)

      billrich

      • Guest
      Re: Using SET and time in batch file, want to get the difference in time
      « Reply #12 on: March 05, 2009, 12:52:08 PM »
      Timercur,

      I modified "timeinseconds.bat "  in post two of this thread.  I cannot reach you by email.

      Good Luck

      Bill in OKC

      marvinengland



        Hopeful

        Thanked: 11
        Re: Using SET and time in batch file, want to get the difference in time
        « Reply #13 on: May 06, 2010, 08:33:33 PM »
        I want to find out how long a process took by accessing either time or some other object at the beginning of the process, then again at the end and compare them to see how long it took.


        Using SET and time in batch file, want to get the difference in time

        http://www.computerhope.com/forum/index.php?topic=78053.0


        Output:


        C:\>timeinseconds.bat
        start time=13:24:37.71
        Hour=13
        minutes=24
        Total Seconds in given minutes=1440
        Total seconds in given  Hours = 46800
        seconds= 37
        start time in seconds =48277
        end time=13:24:47.97
        End Hour=13
        minutes=24
        Total Seconds in given minutes=1440
        Total seconds in given  Hours = 46800
        seconds= 47
        end time in seconds =48287
        Total Time used in seconds = 10
        C:\>type timeinseconds.bat
        Code: [Select]
        @echo OFF
        rem start - end  time

        REM time format 12:46:26.94

        echo start time=%TIME%

        set HH=%TIME:~0,2%

        echo Hour=%HH%

        set MM=%TIME:~3,2%

        echo minutes=%MM%

        set /a MM=%MM% * 60

        echo Total Seconds in given minutes=%MM%


        set /a HH=%HH% * 3600

        echo Total seconds in given  Hours = %HH%

        set SS=%TIME:~6,2%

        echo seconds= %SS%


        Set /a start=%HH% + %MM% + %SS%

        echo start time in seconds =%start%

        echo enter time to sleep

        set /p  sleep=

        sleep %sleep%

        Rem end time in seconds

        echo end time=%TIME%

        set HH=%TIME:~0,2%

        echo End Hour=%HH%

        set MM=%TIME:~3,2%
        echo minutes=%MM%

        set /a MM=%MM% * 60

        echo Total Seconds in given minutes=%MM%


        set /a HH=%HH% * 3600

        echo Total seconds in given  Hours = %HH%

        set SS=%TIME:~6,2%

        echo seconds= %SS%


        Set /a end=%HH% + %MM% + %SS%

        echo end time in seconds =%end%

        set /a totaltime=%end% - %start%

        echo Total Time used in seconds = %totaltime%
        USA

        Quantos



          Guru
        • Keep Calm - Press Play
        • Thanked: 169
          • Yes
          • Yes
        • Certifications: List
        • Computer: Specs
        • Experience: Guru
        • OS: Linux variant
        Re: Using SET and time in batch file, want to get the difference in time
        « Reply #14 on: May 06, 2010, 08:35:21 PM »
        Marvin, this is for you.

        [recovering disk space - old attachment deleted by admin]
        Do You Think When You Act Or Just Act Like You’re Thinking?