Code patternsInput and outputPrint a constant string

Print a constant string

Prints a fixed text with as short a program as possible.

Watch it run

The form “A few cells seeded by one loop” works on 7 cells; the named ones are “counter”. Press Play, drag the step slider forwards and back, or edit the text:

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

A letter is printed by making its code in a cell. Making each code from zero is long: "H" alone is 72 plus signs.

Shorter: keep the cell and walk from one letter's code to the next. "l" to "o" is three plus signs.

Shorter still: one loop first puts a round number in each of a few cells (near 30 for spaces, near 70 for capitals, near 100 for small letters). Each letter is then a short walk from the nearest cell. The classic "Hello World!" program is built this way.

The code

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

+++++++++++[>+++>++++>+++++++>++++++++>+++++++++>++++++++++<<<<<<-]>>>-----.>>++.>--..+++.<<<<.<-.>>>-.>>.+++.------.<-.<<<<+.
Open in the playground126 characters

What it needs

Common mistakes
  • Clearing between letters. The whole gain comes from keeping what is there.
  • Counting on wrapping to reach a far letter the short way round: that makes the program depend on 8-bit cells.

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
A few cells seeded by one loopthe length of the program is what counts hereabout 2 per letter
One cell, from letter to letterthe length of the program2 per letter
Every letter from zeroabout 5 per unit of every letter code3 per letter
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
A few cells seeded by one loopthe text = "Hi"8412725
A few cells seeded by one loopthe text = "Hello, World!"812667625
One cell, from letter to letterthe text = "Hi"81071074
One cell, from letter to letterthe text = "Hello, World!"836636625
Every letter from zerothe text = "Hi"81855356
Every letter from zerothe text = "Hello, World!"81,1813,41339

Other forms

One cell, from letter to letter

No loop at all.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.
Open in the playground366 characters
Run this form step by step

The form “One cell, from letter to letter” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the text:

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

to measure against Every letter from zero

The naive way, to measure against.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++.[-]
Open in the playground1,181 characters
Run this form step by step

The form “Every letter from zero” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the text:

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.