Does anyone know why? I imagine that not having to read the I would help a tiny bit, but the time difference seemed far too great to be that alone.
Short Answer:
It's quite simple. Looking for a NEXT
stack entry with a specific variable takes more time then just looking up the last NEXT
- and beingpart of a loop makes it even more costly.
Detailed Answer:
Each FOR
/NEXT
loop creates a frame on the (BASIC) stack - like GOSUB
, ON ERR
or alike does. When a NEXT
without a variable name occures, the last next-frame is searched and handled.
Now, if one (or more) variables are present, also the first NEXT
entry is searched, like before, but further the variable needs to be parsed from the command, then searched in the variable table and finally compared to the one used in the stack entry found (fine details see below). If not equel, it gets repeated. This difference might sound small, but it adds up with every iteration.
Deeper Details:
(MS-) BASIC stores variable names within the tokenized program as character strings. To look up a variable from the name the string need to be searched in the variable tab, resulting in a pointer to the entry (and thus the value).
A for-stackframe stores, beside things like start value, sign or step value, the address of the variables value (within the variable table). So to compare if a variable given at NEXT
is the same, it has first to be searched and then the addresses to be compared.
Bottom line: Finding a variable takes time
All of this is the reason why not only NEXT
without a variable is faster, but also why speed of NEXT
handling varies with the variable used. With an early declared variable, it will be faster than with a later one. Therfore a good old 1 LET I=0
at the beginning of a program using I
as loop variable will speed up tings - the same way as the general habit of declaring often used variables in (MS-)BASIC will do.