Logic code calculation optimization

Without considering object orientation, let’s recall the three structures in C.

  1. Sequential structure
  2. Branching (conditional) structure
  3. Loop structure

Any combination of these three structures makes up the logical code (algorithm) we write. The following are the three commonly used constructs for tuning. Note that the language used below is JavaScript, but consider most languages.

Code in the computer is actually executed line by line, branch and loop structure after the use of jump statement to achieve the jump, in fact, also become a sequential structure. If the code executes in a sequential structure, you can make the code execute sequentially by incrementing the program counter. If it is a jump statement, additional steps such as addressing, computation, and so on take place.

Here’s a normal loop written in C code. But when actually executed on a computer, it looks like the goto version on the right. Each GOto increases the computation of the computer, so use sequential structures whenever possible for performance reasons.

To optimize the cycle

Common cycles are

  1. while
  2. for
  3. do… while

Note that the loop starts with while and for, do… While is run once and judge at the beginning of the loop

Two scenarios are tuned in the loop

  • Reduce the number of transactions processed per loop
  • Reduce the number of cycles

In a loop judgment, we usually judge the properties of an object for example

var i = 0; for(; i < array.length; i++) { ... }Copy the code

Actually the statement in the computer is

  • Initialize (once)
    1. Initialization of I once (var I)
    2. An assignment of I (I = 0)
  • Judge (n times)
    1. I value lookup (I)
    2. Array value lookup (array)
    3. Find length from array (array.length)
    4. (I < array.length)
  • Post-processing, self-increasing (N-1 times)
    1. I value (I)
    2. I + 1(I + 1)
    3. Assign the result to (I = I + 1)

This judgment actually consists of four statements. Look at the code below

var i = 0; var len = 0; for(len = array.length; i < len; i++) { ... }Copy the code

In this judgment actually the computer statement is

  • An I value lookup.
  • A len lookup
  • Find Length by array
  • I compares with length

Here we have one less statement in a single loop. In multiple loops, n computer statements will be reduced

If it’s a simple numerical comparison we can also use a reverse loop

var i = array.length;
for(;i--;) {
  ...
}
Copy the code

This time the actual statement in the computer is

  • Initialize (once)
    1. I initialization (var I)
    2. Array value lookup (array)
    3. Find length from array (array.length)
    4. I assignment (I = array.length)
  • Judge (n times)
    1. I value (I)
    2. I minus one.
    3. Assign the result to (I = i-1)
  • Post-processing, self-increasing (N-1 times)

There are actually quite a few such statements, but only in order independent and numerical comparisons. This is because some languages provide automatic conversion between values and booleans.

Reduce recursion

When we talk about loops, we also talk about recursion. Recursion in its simplest form is when a function continues to call another function, usually calling itself.

A process. There are data areas, stacks, PCBS, etc. The stack size is basically determined when the process is started, and the stack can store local variables and function call information at run time. If recursion occurs, content is continuously added to the stack. If a local variable is required to execute a function, or if the call is not the last line. An interrupt will occur. After the interrupt, the context of this run is also stored in the stack, forming a call stack.

Occurrence of an interrupt, will trigger CPU interrupt processing, save interrupt information site, instruction jump. When the recovery will jump instruction, restore the interruption of the scene information. Don’t use recursion if you don’t have to.

The stack size is fixed. If you exceed the stack size, there is a risk of stack overflow (the famous Stack overflow). Some languages have tail-call optimizations that allow recursion without saving interrupt information. Tail call optimization – Ruan Yifeng

Optimal selection

Common choices are

  • if… Else is better for range matching
  • switch… Case is more suitable for single-value matching

Whether you use if… Or else the switch… Case, judgments are executed in written order. So we should put the most likely scenario first.

Since the selection structure also triggers instruction jumps, we should use nested if… Else, use the tree form, so as few as possible to get to each result.