Code patternsAdvanced: wrapping cells of a known widthOne copy of code over many frames

One copy of code over many frames

Runs one piece of program at many places on the tape, so the program text holds it once instead of many times.

Watch it run

The form “y = 3 × x in every frame” works on 19 cells; the named ones are “scratch”, “marker”, “x”, “y” and “tmp”. Press Play, drag the step slider forwards and back, or change frames and first x with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

A program that is written out for 512 places is 512 times as long. Length is a real cost here: the chat program is a file you have to load.

Moves in brainfuck are relative, so a piece written for the first place works at every place. The tape is cut into equal stretches, frames, and a loop does the piece and then steps one frame on.

One flat loop first puts a marker in every frame. The walking loop takes the marker away as it goes and stops at the first frame without one. That costs 2 steps more per frame than writing the piece out.

The code

This is the program the figure ran, written by the generators for these values and this cell size. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

+[->>+>>>>+>>>>+>>>>+<<<<<<<<<<<<<<]>>[->[->+++>+<<]>>[-<<+>>]<<<>>>>]
Open in the playground70 characters

What it needs

Common mistakes

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
y = 3 × x in every framegrows with the values3 + 4 per frame
The naive way: written out per framegrows with the values2 per frame
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
y = 3 × x in every frameframes = 4; first x = 287033319
y = 3 × x in every frameframes = 8; first x = 2810693535
y = 3 × x in every frameframes = 64; first x = 2861037,923259
The naive way: written out per frameframes = 4; first x = 28932638
The naive way: written out per frameframes = 8; first x = 2818579716
The naive way: written out per frameframes = 64; first x = 281,47336,833128

Against the naive way

The same job written out once per frame. The steps are nearly the same; look at the length of the text.

The numbers, row by row
this pattern, characters
610the naive way, characters
1,473

This pattern against the naive way, on 8-bit cells, measured when this page was written. The bars show the last row: the length of the two program texts.

ValuesOptimised stepsPlain stepsCharacters
patternnaivepatternnaivepatternnaive
frames = 8; first x = 23516935797106185
frames = 64; first x = 225912837,92336,8336101,473

Measured in the project: one clamped rounding step looped over 64 cells is one copy of the text (about 6,000 characters) and a few extra steps per cell, instead of 64 copies. measured in the project

Other forms

to measure against The naive way: written out per frame

The same piece, n times in the text.

>>>[->+++>+<<]>>[-<<+>>]>>[->+++>+<<]>>[-<<+>>]>>[->+++>+<<]>>[-<<+>>]>>[->+++>+<<]>>[-<<+>>]
Open in the playground93 characters
Run this form step by step

The form “The naive way: written out per frame” works on 18 cells; the named ones are “scratch”, “marker”, “x”, “y” and “tmp”. Press Play, drag the step slider forwards and back, or change frames and first x with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.