3

I often use :: to initiate a comment. Now I had a IF block and I found multiline :: comments do not work in *.bat files if they are in a IF-Block. I found this site https://www.robvanderwoude.com/comments.php and this explains a lot about comments in *.bat files.

normally comments are initiated by a REM command. as the site says... on each REM command the cmd interpreter will ignore the line but re-read the whole file to get the next line after the REM-line, and this will slow down the whole process.

but :: -comments will be interpreted as a invalid GOTO-Label and so the interpreter just jumps to the next line and will not re-read the file and is there for faster processed.

now I also hat a other idea which is not discussed on this site

to create comments with a false IF-Block

IF defined COMMENTBLOCK (
    Write
    Your multiline
    comment here.
)

because the variable COMMENTBLOCK is not defined the interpreter will never look inside the block of the IF command and so it will just jump over the comments inside of the block.

now on this site the guy is talking about speed of a process. I think the interpreter will check the variable COMMENTBLOCK and since it is not defined read all lines until the end of the block and then go on with the process it will, as I think but not am sure of, not re-read the *.bat file as if I would use REM commands, and it would work inside of a IF or FOR block in opposite to :: comments which do not work in IF or FOR blocks if the comment is multiline.

now how can I measure the speed of a *.bat file to find the difference in speed between each method of comment? How can I be sure that...

IF defined COMMENTBLOCK (
    Multiline
    Comment
)

... is fast as

:: Multiline
:: Comment

and how can I prove that ...

REM Multiline
REM Comment

... will be slower than the other methods of commenting?

Such *.bat files are extremely fast ... the difference may is below a second the %time% variable shows me hours minutes seconds and hundreds of a second ... but to over jump such a comment does even seem to take less time ... so the result is really just a logical estimation at all.

also I do not have floppy disks to see what happens if the methods are used from floppy.

may the best would be if some one who knows it could explain what the interpreter does if a false undefined IF-Block appears ... does it act like false labels :: or REM? and what would may be a disadvantage of false IF-Blocks to create multiline comments inside of FOR and IF-Blocks?

Thanks in advance for your clarification...

UPDATE: the method with undefined variables as COMMENTBLOCK can not handle pipes | or closing brackets ) ...

IF defined COMMENTBLOCK (
    )
    |
    This will fail!!!
)

the method with :: and REM can handle every thing including pipe |

:: |
REM |

.. but :: -comments can not be used inside of blocks as IF and FOR

a other strange method for multiline comments is to place two commands in one line as echo sometext & pause but instead of the pause command you need a invalid goto label :: and then add new lines with ^ at the end, this will need a blank line between every comment line and to keep the lines together each line must get a line break with ^

echo some command &:: ^
_^
Strange characters do work ^
_^
but quotes are sensitive, they must be even ^
_^
last line of comment

a other nice method to create multiline comments is to use %= comment =% lines...

%= multiline =%
%= comment   =%

... this also has its drawback, %, !, %~ inside of comments will confuse the interpreter, strange characters can be escaped awkwardly %= %% ~f0 =%

see also the comments below for more information and thanks

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Mugolumbu
  • 31
  • 1
  • 6
  • most of the understanding you seek is explained [here](https://stackoverflow.com/a/4095133/12343998). The general way of testing the speed of something in batch is to repeat it a sufficient number of times in order to determine a viable baseline between the things your comparing. – T3RR0R Oct 03 '20 at 17:11
  • For an alternative method of multiline commenting, which you have not yet considered, take a look at [this](https://www.dostips.com/forum/viewtopic.php?p=23452#p23452) – Compo Oct 03 '20 at 17:23
  • Even if variable `COMMENTBLOCK` is not defined, the block is still read; place the invalid syntax `echo|` inside of the block and you'll get an error… – aschipfl Oct 03 '20 at 18:53

1 Answers1

3

on each REM command the cmd interpreter will ignore the line but re-read the whole file

This seems to be wrong, even for the old command.com (MS DOS 6.22).

But REM is still slower, but it depends on the usage.

Inside a FOR block, the difference is extreme, because the REM will be parsed and executed in each loop, but the :: (also the %= =% comment style) will be parsed and completely removed from the code.
For a heavily repeated block REM takes time, but :: takes no time at all!

In normal code, the difference is only for parsing and a single execution of the comment.
But even there the REM takes approx. 50% more time.

Example:

set "start=%time%"
FOR /L %%# in (1,1,1000) DO (
  call :test
)
set "end=%time%"
call :showdiff "%start%" "%end%" -<loop-call-overhead constant>
exit /b

:test
REM 1
REM 2
REM 3
REM 4
REM 5
REM 6
REM 7
REM 8
REM 9
REM 10
exit /b

The loop executes 10000 times REM.
The loop-call-overhead-constant is measured when the :test function is empty (only contains exit /b) The REM version takes ~3000ms vs ~2000ms for the :: version.

On my system a single REM takes ~0.3ms vs 0.2ms for a ::

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Did the ":: version" have 10 lines of comments that started with "::" instead of "REM" for this test comparison? That appears to be the difference between the displayed script with REM lines and the :: version, that was not displayed. – JohnH Aug 31 '21 at 14:40
  • @JohnH Yes, you're right. I didn't want to show so much lines without any benefit. And adding the `:: version` with another label isn't a good idea, because the file position of the label itself is relevant for the measured time, therefore the block has to be replaced – jeb Aug 31 '21 at 17:40