Why does my script run at half speed after the first run

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 357
Joined: 04 Oct 2008 09:49

Why does my script run at half speed after the first run

#1 Post by alan_b » 26 Feb 2012 16:28

The Script TickTock.bat below can make a very rapid measurement of a interval of time between one %TICK% or %TOCK% and the next %TOCK%
It does this by executing code within a variable, and is about 15 times faster than when I used "CALL :TOCK" to run code at the bottom of the script.

It can perform 50 off %TOCK% measurements in about 60 mSec (1.2 mSec per measurement) when
a) I launch the script by double clicking the file in Windows Explorer ;
b) Launching CMD.EXE and invoking "TickTock #", which will repeatedly START a second instance of itself to do the 60 mSec job
c) Launching CMD.EXE and invoking TickTock - BUT ONLY ONCE PER INSTANCE - once it has run this same instance of CMD.EXE takes 130 mSec

The third line of code is "SET > SET.TXT" and this is subsequently renamed.
There is absolutely no difference between the Environmental Variables captured at the start of the script when launched by modes (a), (b), or (c and repeated)
i.e. when it runs at half speed the Environmental Variables were NOT affected by previous history/experience of this same instance of CMD.EXE

Why does it go slow after the first operation ?
Does CMD.EXE keep the variables in sorted order, and when my variables are added and values changed the memory locations change and gaps get left,
and that "experience" and extra gaps actually hinder instead of help future use of the same variables ?
Should I prefix all my variables with lower case 'z' so they go at the end and cause no gaps ? or is it sorted reverse order ?

I would appreciate advice and suggestions on how to fix the code so that having launched CMD.EXE and run TickTock once,
I only need to hit the up arrow and then hit Return to rerun the same code in the same instance - at full speed.

Incidentally, there are 244 character on the line starting
I tried splitting into two lines with a '^' at the end of the first line, but that just broke my code.

Enhanced TickTock.bat now appears in post below.
It still behaves exactly as the first version.

Last edited by alan_b on 27 Feb 2012 06:15, edited 2 times in total.

Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why does my script run at half speed after the first run

#2 Post by alan_b » 27 Feb 2012 06:00

My previous code was fully functional and did the job, but was rushed out before I went to bed.
I have now seen and corrected some typos and greatly improved the structure of the code.
The results with my new code are identical to the old code.
To avoid confusion I am placing my new code here, and removing the old code from the post above.

Code: Select all

ECHO FILE %0 CREATED 20:12 24/02/2012   Launched %TIME%

REM  ----------------------------------------- SPECIAL START SPEED C0NTROL CODE BEGINNING
IF "%1"=="#" ECHO STARTing %0 :PROC & START "" /LOW /WAIT %0 :PROC & EXIT /B
IF "%1"==":PROC" CALL %1 & EXIT
REM  ---------------------------------------- SPECIAL START SPEED C0NTROL CODE CONCLUSION

SET "TICK=SET "T_X=!TIME!" & FOR /F "tokens=1-4 delims=:." %%d in ("!T_X!") do (SET /A V_X=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100)"
SET "TOCK=SET "T_1=!T_X!" & SET "V_1=!V_X!" & SET "T_X=!TIME!" & FOR /F "tokens=1-4 delims=:." %%d in ("!T_X!") do (SET /A V_X=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100) & SET /A DELAY=(!V_X!-!V_1!) & ECHO !T_X! - !T_1! = !DELAY!0 mSec!T_Str!"

SETLOCAL EnableDelayedExpansion

ECHO This measures the number of 10 mSec ticks during an interval
ECHO from either a %%TICK%% or a %%TOCK%% until a subsequent %%TOCK%%.
ECHO( & ECHO %%TICK%% makes no report, it only captures the 10 mSec clock reading
ECHO %%TOCK%% reports delay since previous clock reading and takes a new clock reading,
ECHO and appends %%T_Str%% or any text following %%TOCK%% - e.g. as "Cause of Delay/etc".
ECHO( & ECHO On my system %%TOCK%% takes about 1.5 mSec, 10 times faster than CALL code

SET "ANS=" & SET /P ANS= "Please Enter a NEW Name : "
rem SET "ANS=" & SET /P ANS= "Please Enter a Name : "
SET "T_Str= Your time entering the Name %ANS%"
SET "T_Str= Code time Renaming SET.TXT to SET%ANS%.TXT
ECHO --------------- Now do 50 Off invocations of %%TOCK%% ----------
SET "T_Str= ."
%TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK%
%TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK%
%TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK%
%TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK%
%TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK% & %TOCK%
ECHO --------------- 50 Off invocations of %%TOCK%% Now done --------
SET "T_Str= = 50 OFF %%TOCK%%
%TOCK% = Time Measurements


As previously, my special START code includes this - now on line 6

Code: Select all


I observe no difference in speed if I remove the option switch /LOW
You may find a difference on your machine - especially if it is single core and busy on other things as well.

The very first time I ran my code I quickly regretted that I had not thought to use /LOW
My first code was flawed and START at normal priority launched the second instance and that concluded by doing what was hoped for,
BUT - before it did what it should have done it STARTed another instance,
and my system ran away from me with an exponential increase in the number of simultaneous instances of my code - recursion on steroids.
Very occasionally I could use TaskManager to select an instance of CMD.EXE and click the CLOSE button,
and I caught a glimpse of a CONFIRM prompt,
but several more instance of my code were concealing the prompt and taking focus before I could respond.

The madness stopped a minute or so later when all my 8 GB of RAM had been used up at 1 MB per instance,
and absolutely nothing worked anymore - apart from the front panel Power switch.
I had fears that I would need my Boot recover CD to restore my partition image,
but I am pleased to say the system powered up with the only abnormality that it noted a shutdown failure and offered start-up alternatives,
but normal start-up was free of incident.

As a kindness to others and for your safety in case you choose to experiment and make a similar booboo, I chose to retain /LOW


Post Reply