2

Since there are technically 4 types of loops (for, while, repeat, and goto/lbl), which one is the fastest to use "infinitely"? Here is what I mean:

while 1
End

repeat 0
End

lbl 1
goTo 1

for(n, 1, [number large enough to function as practically infinite]
End

Which of these is the fastest, or is there an even faster one?

Johnny Dollard
  • 708
  • 3
  • 11
  • 26
  • 3
    Every time the calculator gets to a goto statement, it starts at the very beginning of the program and works its way down until it finds the lbl. It's best practice to avoid gotos unless the lbl is at the very beginning of the program. – bearacuda13 Jan 05 '17 at 15:27
  • If it is at the beginning, is it still slower than while/repeat? – Johnny Dollard Jan 05 '17 at 18:21
  • I would assume it is. I don't know for sure, but if repeat 0 then End is three bytes combined, then I would think that lbl1 then goto1 would be four. – bearacuda13 Jan 05 '17 at 18:24
  • Nobody seems to have said the obvious, so I will: all "infinite" loops are equally fast, since all take the same amount of time. – Tommy Feb 16 '17 at 15:03

2 Answers2

5

In terms of both size and speed, the repeat and while loops are the fastest.

While 1 and Repeat 0 are both 2 bytes, while End is 1. In terms of space, they are both 4 bytes.

In terms of speed, the same is true. Just try making a simple program that loops to infinity and time it.

TL;DR: While 1: End and Repeat 0: End

Julian Lachniet
  • 223
  • 4
  • 25
2

Technically the fastest is:

AsmPrgm
18FE

which has to be run as an assembly program.

This is using Z80 assembly to create an infinite (unstoppable except by the reset button) loop. The code is run directly by the processor, so the OS doesn't need to spend time interpreting it.

fuzzything44
  • 701
  • 4
  • 15
  • 1
    "loop: jr loop" is 12 cycles per iteration. "loop: jp loop" is 10. But the fastest Z-80 infinite loop is "ld hl,loop; loop: jp (hl)" at 4 cycles per iteration. However, I don't know enough about TI-84+ BASIC and its AsmPrgm to express those loops which require absolute addresses. – George Phillips Feb 15 '17 at 18:37
  • Good point. The value of `loop` depends on your OS version and differs per calculator. Of course, my answer also requires `Asm84CPrgm` instead of just `AsmPrgm` if you're using a TI-84 with color display. – fuzzything44 Feb 16 '17 at 20:35