|
Answer» 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. Frankwhat does %time% look like?%time% looks like 19:44:16.85 FrankYou 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. o/k, this no longer sounds like a job for a batch file. Frankcan 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. 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. FrankHave you got XP pro?
can you use the systeminfo command?
Yes, I have Windows XP Pro SP2.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+%%JThank 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. Thanksregarding 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 Thanks, Tried it and the overhead on my system is also 2 seconds. 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% millisecondsThat 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
|