On Wed, Oct 26, 2022 at 7:24 PM runrin <run....@rin.run> wrote:

> i made another version that uses string pointers because i felt bad
> using 16 strings. the PEEKS seem to be pretty expensive though, so i
> think it's actually slower than the previous version, but the code is a
> bit clearer. using MID$ is probably faster, [...]


I also noticed that PEEK() seemed surprisingly slow, no faster than using
an array. But I'm surprised to hear that MID$ was faster. I hadn't even
tried that because I just presumed it'd be slow.

string pointers are weird in basic. the pointer returned by VARPTR(S$)
> points to the following:
>
> ptr + 0 = string length
> ptr + 1 = low byte of pointer to string
> ptr + 2 = high byte of pointer to string
>

Ah! That's why my VARPTR attempts were failing. I had presumed the first
two bytes would be the address. Using a length instead of a NULL terminator
is interesting. If Model T strings can hold NULLs, then maybe one could
create a string and change its pointers so it reads from any arbitrary
location in memory. Would using MID$ on that be faster than grabbing the
bytes via PEEK? I wouldn't think so, but I've been wrong before.

[...] i know those extra spaces make my code slower, i'm just formatting it
> that way here so its actually legible.
>

That is another thing that I wonder about. I can see that it saves bytes,
but how much performance does removing spaces actually give? It seems like
it'd only matter in tight loops, so the whole program wouldn't need to be
crunched. My guess is that the speed difference is miniscule, especially
compared to the readability benefits.

On a historical sidenote, in 1979, Microsoft released MBASIC 5.0 for the
Z80. I believe it was the first BASIC Microsoft released that allowed
variable names to be longer than two letters. However, in order to do that,
they had to require spaces so that long variables wouldn't get parsed into
reserved words ("FOR  TRANSUXB AS ICRULZ!"). Did the mandatory spaces cause
a large performance hit? The manual mentions that a version of BASIC is
still available with the old two letter variable names, but it was
advertised as being for computers with tiny amounts of RAM, not for speed.

—b9

Reply via email to