stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 305

Day 305 of 365 · Expert · Month 11: Optimisation and debugging

128 instructions per tick

128 executions per tick: what every line costs, comments and labels too, how long loops are split into portions and where the yield belongs.

Lesson 20 min Week 44

What you learn today

  • Count run time per tick
  • Split long loops
  • yield in the right place

A pressure vessel is being watched, and at the same time the chip should form the average of the last 64 readings. The sum over 64 values needs around 300 executions. If it runs in one go, the overpressure alarm comes two or three ticks too late. For a pressure vessel that is a second too many.

Counting run time per tick

The chip executes at most 128 steps per tick. Then it pauses and carries on at the same place in the next tick – without an error. In the game code every step is a line: comment lines, blank lines and labels the chip runs over cost a step too.

Linecosts
instruction1
comment or blank line in the way1
label jumped to or passed1
yield1, then the tick is over

So a loop with five instructions and one label costs six steps per pass. For 64 values that is 384 steps – three ticks.

Splitting long loops

The solution: the long work is broken into portions. Every tick one piece runs, and a counter remembers where to carry on. Only after the last piece is the result output.

add end i PORTION
portion:
get tmp db i
add total total tmp
add i i 1
blt i end portion
blt i SLOTS start        # not done yet: next tick
div tmp total SLOTS
sbn DISP HASH("Mittel") Setting tmp

Eight values per tick cost around 40 steps. After eight ticks the sum is done, and the chip still has enough room for everything else in every tick.

yield in the right place

The fast part – the overpressure watch – sits right after the yield and therefore runs first in every tick. The portion comes after it. When a portion is done, the chip jumps back to the yield instead of adding the next portion:

  • Watches, switches, safety: every tick, at the front.
  • Statistics, searching, sorting: in portions, at the back.
  • Output of the result: only when all portions are through.
Warning

Without yield in the main loop the chip pauses in the middle of any line. The outputs of a loop can then be half done, and where the next tick begins depends on the length of the loop. A deliberate yield makes the flow predictable.

Tip

Comments inside a loop cost a step on every pass. Write them before the loop or use the game version, which removes them.

Diagram

Ticks and yield on a timeline tick 0 · 0.0 s tick 1 · 0.5 s tick 2 · 1.0 s tick 3 · 1.5 s 5 instr. yield 5 instr. yield 5 instr. yield 128 instr. no yield: limit hit Device values only change between two ticks.
Every tick: first the fast part, then one portion of the long work, then yield.

Exercise

Task

The chip watches the vessel “Kessel” and forms the average of the last 64 pressure values:

  • every tick: light “Druck” on above 200 kPa or without the sensor, then the value into the ring buffer (64 slots)
  • after that sum up 8 buffer slots; after all 64 show the average on the display “Mittel” and start again
  • the watch must run in every tick

Hints

Hint 1

Watch first: sgt, snan, or, sbn. Then skip the storing with bnan.

Hint 2

Portion: add end i PORTION, loop up to end, then blt i SLOTS start.

Hint 3

Only when i reaches 64 output the average and set total and i to 0.

Quiz

3 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere