Five steps for program optimization

Source: Internet
Author: User

Five steps for program optimization
5 Directions of program optimization

80/20 rule: During program execution, 80% of the time is spent on 20% of the Code.
Before optimization, we need to find the Key Path of the 20%;
Specialized tools are available in various languages to find the key paths of these 20%, such as gprof, which is frequently used by C ++;

Refer to performance optimization practices of C ++

Optimize time-consuming computing in key paths;
The main Optimization Direction is:
Reduce repeated computing, pre-calculation, delay calculation, reduce computing costs, and no calculation;

Reduce repeated computing

A typical example is cache. It saves the same computing (query databases and read/write files) and waits for the next operation to continue;
Applicable scenario: the calculation result is valid and needs to be calculated again after a period of time;

Pre-Calculation

Time-consuming computing in key paths is calculated in advance to save the cost of each computing;

  • Pre-calculated comparison table
    The ing table used in the Key Path is pre-calculated and directly used in the Key Path;

  • Advance calculation to Initialization
    For example, the memory allocation time is allocated in advance to the initialization time to establish a memory pool;

  • Advance computation to compilation
    For example, you can use a constant expression to calculate the final value during compilation to save the runtime overhead;
    Related Technologies: Template meta programming;

Applicable scenario: the calculated value remains valid and does not need to be calculated again;

Latency Calculation

Delay the computing time to the later stage. In this way, for exceptions or other branch situations, the time will be converted in the middle and no computing is required;

  • There are many Branch Conditions
    Delay the most time-consuming computing. In this way, many scenarios may be transferred to other branches in the middle, without computing;

  • Skills in condition determination: a | B a & B
    If the judgment condition is time-consuming, more time-consuming will be placed behind the calculation; in this way, for a | B, when a is set, B does not have to calculate it any more; the advantage of delayed computing is that computing is not needed;

Applicable scenario: branch condition scenario;

Reduce computing costs

This is the most direct optimization method that can be thought of. How can we directly reduce the computing cost;

  • Change memory application from Stack to stack
    Dynamic memory allocation is expensive, and the memory allocation is changed from the stack to the stack;

  • Reduced flexibility and replaced library functions with functions of the custom version;

  • Rewrite with lower-level commands or languages;
    Embedded Assembly Language in C ++;
    Use SSE2 and other instruction sets;

  • Use a better algorithm or data structure;
    When operating STL containers, STL algorithms are generally more efficient than self-written algorithms. We try to use STL algorithms to replace our handwriting algorithms;
    Refer:
    Summary of STL range member functions and interval Algorithms
    Efficient use of STL

Applicable scenarios: This type of optimization is generally at the cost of reducing code readability (except for STL), used in the final stage of optimization;

Not calculated

The ultimate solution for optimization, not computing;

  • Business discovery
    Useless business logic and obsolete business logic still exist in the Key Path and are deleted happily;

  • Overhead of temporary objects
    In our code, some temporary objects may be invisible, and eliminating the temporary object will save this part of the overhead;
    Reference: Eliminating temporary objects

The above is the optimization of the Key Path of a single thread. Next, let's talk about the optimization of multi-thread extension to multi-core;

Posted by: Large CC | 06AUG, 2015
Blog: blog.me115.com [subscription]
Github: Large CC

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.