Code complete ch.26 code Adjustment Technology

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.