[Coco] INSTR question

William Astle lost at l-w.ca
Thu Feb 9 17:34:29 EST 2017


On 2017-02-09 02:12 PM, Allen Huffman wrote:
> I never tried to use INKEY$ directly until today, and benchmarks showed it was much faster for this than assigning a variable.

It follows logically that this would be the case. Of course, it requires 
even thinking about it to realize that. The reason it is faster is:

1. It eliminates at least two variable table searches and a possible 
variable table expansion. Variable table searches can be quite slow if 
the variable you are looking for isn't found or is at the end of the 
table. The search is a simple linear scan from the start of the table 
until either the end or the the variable is found.

2. It eliminates a trip through the assignment ("LET") code, which while 
not terribly slow does add some overhead.

3. It eliminates one trip through the expression evaluator. Again, 
that's not particularly show, but it does add overhead.

Also, the check for NULL will add yet another variable table search and 
a run through the expression evaluator.

Do that stuff in a loop and those overheads add up substantially. While 
not a big deal in a "wait for a keypress" loop, it could be in a game 
logic loop where stuff has to happen while waiting.

It also has the advantage that it avoids creating a variable table 
entry, which can be important if you're running tight on memory. (That's 
7 bytes for the variable table entry.)


More information about the Coco mailing list