Klaus Elk Books

Performance and Guidelines

Collected ideas for Performance

The following is a list of performance Ideas. Some stem from one of my favorite books "Code Complete 2" by Steve McConnell:

Other Guidelines for Embedded Systems (especially C-based)

Here are some more guidelines - several from "Code Complete 2" - not so much about performance:

NASA's Power of Ten

The following is a list on "NASA's Power of Ten" Guidelines for C-programs I found somewhere. I don't know whether it's genuine - but it's interesting:

  1. Simple control flow. No jumps or gotos. No recursiveness.
  2. All loops bound by an integer max. E.g. linked-list search.
  3. No use of heap - thus no malloc.
  4. Limit Function size. Max 60 lines – one paper. Single purpose.
  5. Use a minimum of two runtime asserts per function.
  6. Data Hiding. Declare variables at lowest scope.
  7. Check return values. If don’t care (e.g. printf) – signal by casting to void.
  8. Limit use of preprocessor. No ifdefs to various build-targets.
  9. Restrict the use of pointers to one level of dereferencing.
  10. Compile with –Wall –Werror – Wpedantic.

Here's an old NASA motto I picked up years ago:

Test what you fly - fly what you test.

Trick: The Fork (Assembly, Two's Complement)

This is a trick that I learned in Robin Sharp's class at DTU many years ago. It is only relevant if you need to save some time in an inner loop, and you are willing to write at least some instructions in assembly language.

Having worked with DSP in bit-slice and assembler, I have actually used this trick many times.

Problem: You need to know whether a number is within a certain range.


y-axis
|
|
|------- Upper Limit A (Not Included) ----------- e.g. 17
|
| 
| Is X in this range?
|
|------- Lower Limit B (Included) ---------------- e.g 10
|
|
|
-----------------------------------------------------> X-axis
In the register where you have X - e.g. the accumulator - do the following in the relevant assembler language:
Sub A
Add (A-B)
JNC OutOfRange

After each of the first two operations, the carry will be set if the sign changes. In other words, in the above figure, crossing the X-axis gives a carry (incl going from negative to 0 or vice-versa).

Now, after the first two operations, the Carry is set if B <= X < A. This means that you get to do two tests with only one possible jump.

Desktop Test:

X SUB 17 ADD 7
-3 -20,Cy=0 -13,Cy=0
9 -8, Cy=1 -1, Cy=0
10 -7, Cy=1 0, Cy=1
13 -4, Cy=1 3, Cy=1
16 -1, Cy=1 6, Cy=1
17 0, Cy=0 7, Cy=0
20 3, Cy=0 10, Cy=0
Black Elk

© 2026 KlausElk.com & ElkTronic.dk