--- title: "Style and Speed in BASIC" type: "article" slug: "style-and-speed-in-basic" url: "http://localhost/article/style-and-speed-in-basic/" markdown_url: "http://localhost/article/style-and-speed-in-basic.md" published_at: "2020-10-27T17:12:29+00:00" modified_at: "2026-06-21T23:26:21+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "When you write a BASIC program, the order in which you carry out your tasks affects the speed of your program. This routine lets you see simple examples that show how the same task can take 42 seconds at the front of a program , but 78 at the end. When you've seen these results,…" category: - name: "Syntax" slug: "syntax" taxonomy: "category" url: "http://localhost/category/periodicals/syntax/" post_tag: - name: "Best of Timex/Sinclair 2068 Articles and Documents" slug: "ts2068best" taxonomy: "post_tag" url: "http://localhost/tag/ts2068best/" - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" - name: "ZX81" slug: "zx81" taxonomy: "post_tag" url: "http://localhost/tag/zx81/" model: - name: "Sinclair ZX81" slug: "zx81" taxonomy: "model" url: "http://localhost/model/zx81/" - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" publication: "Syntax" publication_r: id: 10246 title: "Syntax" type: "periodical" url: "http://localhost/periodical/syntax/" volume: "5" issue: "11" issues_articles: - id: 26733 title: "Syntax/Syntax ZX80 v5 n11" type: "issue" url: "http://localhost/issue/syntax-syntax-zx80-v5-n11/" pages: "8" pubdate: "November 1984" archive_link: false volumeissue: "v5n11" --- # Style and Speed in BASIC When you write a BASIC program, the order in which you carry out your tasks affects the speed of your program. This routine lets you see simple examples that show how the same task can take 42 seconds at the front of a program , but 78 at the end. When you’ve seen these results, you may rethink your notion to put a screenful of REM’s at the start of your program. Additional Seconds Required for 5000 GOSUBS at Selected Places | Call from | Top | Mid | Bottom | | --- | --- | --- | --- | | Top | X | +6 | +11 | | Mid | +12 | +18 | +23 | | Bottom | +25 | +31 | +36 | Your 2068 and your ZX/TS (in FAST mode) take the same additional time for these tasks. For 2068’s, X=42 seconds. In FAST, ZX/TS’s take only 35 seconds because they use no time to control the screen. In SLOW mode, ZX/TS’s will take 5-6 times longer — both the fastest time and the penalty get magnified. SYNTAX measured these results using the listings reproduced here. Note that content of program lines is irrelevant — only the number of lines to skip affects the time. Also, these programs are not long, but we traverse them many times. You can rerun these tests with your changes. We just changed the target of the GOSUB to 10, 700, or 1000 as desired. We always started with a clear screen and began the run with GOTO 2, 200, or 2000. Next we wondered if FOR-NEXT loops would take variable times, so we deleted lines 3, 300, and 3000, then tested. Sure enough, loops in the middle took 6 extra seconds and loops at the end took 11. Again, the 2068 came out a second slower. SYNTAX suggests the following rules to make your programs faster when that’s your goal. Remember, speed costs readability. - Locate setups, REM’s, and seldom-used pieces at the end. - Put often-used items and their calling routines at the front o Minimize the number of pro¬ gram lines preceding loops. - Trade complex instructions for fewer lines preceding loops. - Avoid subroutines called a lot by a loop near the end of the program — repeat the code in the loop instead. ``` 2 FOR I=1 TO 5000 3 GOSUB 10 4 NEXT I 5 STOP 10 REM 99 RETURN 100 REM 110 REM 120 REM 130 REM 140 REM 150 REM 160 REM 170 REM 180 REM 190 REM 200 FOR I=1 TO 5000 300 GOSUB 10 400 NEXT I 500 STOP 700 REM 799 RETURN 800 REM 810 REM 820 REM 830 REM 840 REM 850 REM 860 REM 870 REM 880 REM 890 REM 1000 REM 1999 RETURN 2000 FOR I=1 TO 5000 3000 GOSUB 10 4000 NEXT I 5000 STOP ```