Home / Microsoft / Microsoft DOS / Using SET and time in batch file, want to get the difference in time
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Using SET and time in batch file, want to get the difference in time  (Read 8364 times)
tlmercur
Topic Starter
Newbie



Posts: 2


« on: March 02, 2009, 10:26:02 PM »

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.
IP logged
Dias de verano
Guest
« 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)
IP logged
billrich
Guest
« 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 » IP logged
tlmercur
Topic Starter
Newbie



Posts: 2


« 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!

IP logged
Dias de verano
Guest
« 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?



IP logged
billrich
Guest
« 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
   
IP logged
Dias de verano
Guest
« 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.



IP logged
billrich
Guest
« 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?





IP logged
Dias de verano
Guest
« 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 » IP logged
billrich
Guest
« 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.
IP logged
Dias de verano
Guest
« 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)
IP logged
Dias de verano
Guest
« 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)
IP logged
billrich
Guest
« 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
IP logged
marvinengland
Hopeful



Thanked: 10
Posts: 258


« 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%
IP logged

USA
Quantos
Guru



Thanked: 158
Posts: 4,041

Experience: Experienced
OS: Windows Vista


Union Proud IW 720

Iron Workers Local 720 1 1
« Reply #14 on: May 06, 2010, 08:35:21 PM »

Marvin, this is for you.

[recovering disk space - old attachment deleted by admin]
IP logged

"Ah the agony, ah the shame, making one man's privates - public for a game..."  Mel Brooks
levidos
Newbie



Posts: 1

Experience: Beginner
OS: Unknown

« Reply #15 on: March 11, 2011, 09:47:53 AM »

Check out this ultimate script that queries system time through WMI.
Link Removed...[/i]
« Last Edit: February 02, 2012, 06:25:12 PM by patio » IP logged
Migxi
Newbie



Posts: 2

Experience: Beginner
OS: Unknown

« Reply #16 on: February 02, 2012, 06:15:33 PM »

Beware of the confusion Octal/Decimal if the variable is 08 or 09

So modification needed...

set T1=%TIME%
echo Start time= %T1%
set HH=%T1:~0,2%
set MM=%T1:~3,2%
set SS=%T1:~6,2%
Rem To remove confusion Octal/Decinal with 08 and 09
IF %T1:~0,1%==0 set HH=%T1:~1,1%
IF %T1:~3,1%==0 set MM=%T1:~4,1%
IF %T1:~6,1%==0 set SS=%T1:~7,1%

Envoy
IP logged
Squashman
Hopeful



Thanked: 25
Posts: 345

Experience: Experienced
OS: Other



« Reply #17 on: February 02, 2012, 08:04:52 PM »

Beware of the confusion Octal/Decimal if the variable is 08 or 09
Love it when people dig up old threads.
Your statement is clearly defined in the help for the SET command.
Quote
Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Using SET and time in batch file, want to get the difference in time « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.138 seconds with 19 queries.