Code patternsAdvanced: wrapping cells of a known widthTable lookup as a decision tree

Table lookup as a decision tree

Gives entry number x of a fixed table, in a number of steps that depends on the size of the table and not on x.

Watch it run

The form “A table of squares” works on 11 cells; the named ones are “x” and “y”. Press Play, drag the step slider forwards and back, or change x with the slider and edit the choice of entries:

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

The table is not on the tape. It is in the program text, as a tree of if and else: first "is bit 0 of x set?", inside each answer "is bit 1 set?", and so on. At the end of each path one entry is added to the result.

The questions are the tests of the bit peel, so no bit is ever stored. A table of 256 entries is 8 questions deep: about 70 steps.

The price is length. Every entry has its own path in the text. The table of 16 squares here is about 4,000 characters; with 256 entries it is 134,000.

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 playground4,035 characters

What it needs

Common mistakes
  • Using it for a big table. The text grows with the table; the tape form, store and fetch by index, does not.
  • Using a tree where x is nearly always tiny. A chain of "take 1 off, still not zero?" is shorter and then just as fast: that is the form The naive way: a chain on this page.

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.
FormPlain stepsOptimised steps
A table of squaresthousandsabout 40 for 16 entries, 60 to 80 for 256
The naive way: a chaingrows with xabout 4 per unit of x
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
A table of squaresentries = 16; x = 5324,035more than 30,000,00038
A table of squaresentries = 256; x = 332134,250more than 30,000,00061
A table of squaresentries = 256; x = 10032134,250more than 30,000,00069
A table of squaresentries = 256; x = 25532134,250more than 30,000,00082
The naive way: a chainentries = 16; x = 53232815223
The naive way: a chainentries = 256; x = 33266,3288815
The naive way: a chainentries = 256; x = 1003266,32812,407403
The naive way: a chainentries = 256; x = 2553266,32871,1521,023

Against the naive way

The same table as a chain: take 1 off x and add the next difference, once for every unit of x.

The numbers, row by row
this pattern: entries = 256; x = 255
78the naive way: entries = 256; x = 255
1,023

This pattern against the naive way, on 16-bit cells, measured when this page was written. The bars show the last row: optimised steps.

ValuesOptimised stepsPlain stepsCharacters
patternnaivepatternnaivepatternnaive
entries = 256; x = 35715296,37788133,19866,328
entries = 256; x = 100654038,060,64712,407133,19866,328
entries = 256; x = 255781,02320,528,68471,152133,19866,328

Measured in the project for a table of 256 entries and a random x: about 68 optimised steps with the tree, against about 500 with the chain. measured in the project

Other forms

to measure against The naive way: a chain

Nested loops, one per entry. Short text, and a cost per unit of x.

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

The form “The naive way: a chain” works on 4 cells; the named ones are “x”, “y” and “counter”. Press Play, drag the step slider forwards and back, or change x with the slider and edit the choice of entries:

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.