Optimization of general ideas/modes (1)

Source: Internet
Author: User
Document directory
  • 1. Space Change Time
  • 2. Cache)
  • 3. hotspot
Code optimization is a very technical activity. There is no common method, but some common ideas/modes can be summarized. Below we will record some of my ideas/patterns, or there may be crossover.
1. Space Change Time

This is the most common optimization method. In general, code optimization is the best in time and space. Today, the rapid reduction of storage costs leads to the more common problem of time optimization. Space-for-time refers to the data generated by computing units during the runtime period, which is generated in advance and stored in external or internal memory in encoding, compile-time, or runtime, it can be obtained directly when necessary without computing. The implementation of the template Technology in C ++ can be said to be an example of changing the space for time. The C ++ compiler generates a set of corresponding code for all types of template classes used in the program, this increases the file size, but object reference generic operations like Java are faster (avoiding type conversion, value-type boxing, and unboxing operations ).
2. Cache)

Cache Technology can be said to be a special case of space for time. It stores the data that requires repeated computation after the first computation and queries whether the data is cached before re-calculation, if the cache is used, it only needs to be obtained from the memory without computing. It is also a "space for Time" method, the reason for its independence, one is because this method is basically a must for optimization, and the other is because it emphasizes "dynamic ", the third reason is that the cache is not only used to change the space for time ".
The cache is generated dynamically during the first computation, that is, the cache content is created at runtime. The cache generated each time does not necessarily last until the process is terminated, because the cache will "expire" due to changes in various States ". Therefore, it emphasizes dynamic. Because of its dynamic nature, it has some advantages in deployment, maintainability and flexibility compared with the "static" Space-for-time method.
What is the function of caching besides changing the space for time? There is also a very important role-to reduce the latency impact on speed. Whether it is an external memory (hard disk) or an internal memory, there is a delay in addition to the data transmission time during data access. The time for obtaining (writing) data is equal to the data transmission time + delay time. If a computation obtains a small piece of data multiple times, the efficiency will be very low. In this case, you can obtain the data in the low-speed device from the whole block to the high-speed device cache (usually the hard disk cache to the memory, and the memory cache to the cache in the CPU ), this greatly reduces the performance loss caused by latency.
Cache is widely used, and cache technology appears almost as long as code needs to be optimized. A well-known example is dynamic web page caching. The server dynamically generates static pages when a user requests a dynamic web page for the first time, after that, users' access points to the generated static pages (of course, there are also factors such as cache invalidation and expiration that need to be considered ).
3. hotspot

Many articles will mention the 80/20 rule in optimization, that is, to find the most time-consuming 20% code for optimization. The 20% code is a hotspot. This is an idea of optimization, but other methods are needed for optimization. The hotspot technology in Java virtual machine is the specific implementation of this idea. It analyzes the running status of JAVA byte code, finds the most time-consuming code, and converts it into native code to improve the speed.

Optimization of general ideas/models (2)

Optimization of general ideas/models (III)

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.