What?
Methods to Improve code running speed and reduce code resource occupation
Why?
All the methods mentioned here are "available". Some may not work in your environment at all, and some may actually produce good results.
How? Logic-Logic
- Stop judging after knowing the answer
- Adjust the judgment sequence according to the frequency of occurrence: Make the fastest running and the most likely to make true judgments first, that is, make the program easier to process in common situations.
- Use a query table instead of a complex expression
- Evaluate inertia: lazy loading
Loop-Loop
- Exclude judgment
- Merge: cyclically merge two identical counters
- Expand: After the loop is fully expanded, it will be faster
- Minimize the amount of work done inside the loop
- Sentry Value
- Put the busiest cycle in the innermost part
- Reduction intensity
Data Transformations-Data Transformation
- Integer number instead of Floating Point Number
- As few array dimensions as possible
- Minimize array references
- Secondary Index
- Use Cache
Expressions-expression
- Using Algebraic Equations: instead of judging SQRT (x) <SQRT (Y), it is better to judge x <Y
- Reduce computing strength
- Use addition instead of multiplication
- Replace power with multiplication
- Use the triangle constant equation to replace the equivalent trigonometric function
- Use long and INT instead of Longlong Integers
- Replace floating point with fixed point or integer number
- Replace double precision with single precision
- Replace multiplication 2 or Division 2 with shift operations
- Initialization during compilation: Use 0.693 instead of log2
- Be careful with system functions
- Use the correct constant type: type conversion during runtime will produce a large amount of consumption
- Calculate the result in advance
- Delete common subexpressions
Routime-subroutine
- Use inline
- Rewrite key code in a low-level language