Page 1 of 1

Batch command execution times

Posted: 16 Jan 2012 00:55
by Rileyh
I have recently done a test with a script that I have written.
It "interpret strings and acts in response" (see my thread a few pages ago) and I did a test with it to see how fast it ran.
I got it to echo the time (echo %time%) and then echo "Hello" 100 times. It then echoed the time again and I calculated the run time.
I did a test with standard batch and I got the time as 3 milliseconds on a slow processor. With this code for interpreting:

Code: Select all

@echo off
set "$file=test.txt"
set "count=0"
:loop
set "count=%count% + 1"
set /a count=%count%
for /f "tokens=1,2* delims=: " %%a in ('findstr /n /i "printline" "%$file%"') do (
  set "command=%%b"
  set "value=%%c"
  setlocal enabledelayedexpansion
  echo !value!
  endlocal
  goto :break
)
:break
if "%count%"=="100" (echo %time% & pause >nul)
goto :loop

I clocked it at 9 seconds on a Pentium 4 2.4ghz Single Core processor and 3 seconds on a Intel Core i5 2.8ghz Quad Core processor.

MY QUESTION:
This code (slightly modified) will be used multiple times in the same batch file and so as it gets longer it will run slower. Is there a way to make it much faster, for example down to less than a second?

Regards,
Rileyh

Re: Batch command execution times

Posted: 16 Jan 2012 07:13
by Squashman
Most of that is dependent on your input data and also if you are running the script from a Network resource. If your input file is thousands of lines long it could take a while to find your search argument in your file and I can guarantee it will probably take 3 times longer on a Network Resource. I run batch files on files are usually well over 100,000 lines and sometimes millions and I always pull the data to my local hard drive first.

Re: Batch command execution times

Posted: 16 Jan 2012 09:47
by Aacini
You may speed up any Batch file a little if you carefully choose the shortest/simplest forms of commands. In your example above, change these lines:

Code: Select all

set "count=%count% + 1"
set /a count=%count%
by this one:

Code: Select all

set /a count+=1
Also, avoid SETLOCAL/ENDLOCAL command pairs when possible; try to modify your FOR loop by another one that read the data via SET /P.

Finally, you may get important time savings when processing a large file if you avoid GOTO and use WHILE macro instead to achieve loops.