PB code optimization

zhaozj2021-02-08  200

PB code optimization

Today's computer running speed is already very fast, and because the boss often reads a tightening spell in the ear, we are interested or unintentionally forgot to optimize our code, as long as you can complete the task (I also). However, when we are idle, you will also take a look at whether our code needs to improve. Below is a few cases I think it is worth optimization.

The first case:

IF condition1 and condition2 Then

// Code Goes Here.

END IF

with

IF condition1 Then

IF condition2 Then

// Code Goes Here.

END IF

END IF

For the first way written, since the PB compilation method is different from a common way, the condition 2 is calculated regardless of the value of the condition 1. In this way, when the condition 1 is FALSE, the condition 2 may be calculated. In terms of random probability, it may be more than half of the operation. Therefore, for most situations, I think it is best to write in the second way. Of course, special circumstances is there, that is, you really want to calculate the condition 2. Similarly, the same is true for OR.

IF condition1 or condition2 then

// Code Goes Here.

END IF

with

IF condition1 Then

// Code Goes Here.

Else

IF condition2 Then

// Code Goes Here.

END IF

END IF

The second case:

IF not conditionen

// Code Goes Here.

END IF

with

IF condition the

// no code goes here.

Else

// Code Goes Here.

END IF

For the previous method, the conditional expression returns false and performs the following code. This may perform a non-computation relative to one of the following writing methods. If you have any questions, you may wish to test the following example:

// Small test: The judgment criteria is just to expose, the actual situation may be more complicated.

Long i // counter

Long ll_start // execution start time

Long LL_USED1 / / mode is time consuming

Long ll_used2 // mode is time consuming

//method one

LL_Start = CPU ()

For i = 1 to 900000

IF not (1> 1) THEN

i = i

END IF

NEXT

LL_USED1 = CPU () - LL_Start

// mode two

LL_Start = CPU ()

For i = 1 to 900000

IF 1> 1 THEN

Else

i = i

END IF

NEXT

LL_USED2 = CPU () - LL_Start

//View Results

IF ll_used2> ll_used1 then

MessageBox ("Tip", "The former is running short!")

Else

MessageBox ("Tip", "The latter is running short!")

END IF

Some people may say, with the following way, if there is no code under the conditional expression, then there is no code, which looks not very comfortable. It is indeed this. Therefore, when we are written to the above way, try not to use the NOT operation, while maintaining the conditional expression itself returns the desired TRUE value. The third case:

IF condition1 Then

// condition1

Elseif Condition2 Then

// condition2

Elseif Condition3 Then

// Condition3

Else

// other

END IF

with

Choose Case / * Expression * /

Case / * item * /

/ * StatementBlock * /

Case / * item * /

/ * StatementBlock * /

Case Else

/ * StatementBlock * /

End chaoose

For a form of expression, I think we generally don't consider the order of order. However, in fact, we should put the most likely, in front, this can avoid the place where we truly need to run the code after judging a lot of conditions.

The fourth case:

For ... to ...

IF condition the

// true

Else

// false

END IF

NEXT

with

IF condition the

// true

For ... to ...

// Code Goes Here

NEXT

Else

// false

For ... to ...

// Code Goes Here

NEXT

END IF

Although the way of writing below seems to seem to be more code, but it greatly avoids the conditional judgment in each cycle. In fact, one principle is that we should try to avoid conditional judgment in the cycle, but to determine the conditions outside the circle.

In fact, it is true for the PB language, that is, the first case, for the three cases, for other programming languages, I think it also applies.

This is my understanding, and the fallacy, please refer to you.

转载请注明原文地址:https://www.9cbs.com/read-1799.html

New Post(0)