Practical development experience: How to reduce CPU usage

Source: Internet
Author: User

In software development and performance testing, CPU utilization is a very important indicator, in the end what factors will lead to higher CPU utilization? What other means can reduce the CPU occupancy rate? This article is "Jhuster's column" of "actual combat development experience" series of another article, a brief summary of the CPU utilization of those things.


1. How do I test CPU usage?


First of all, we have to learn how to test the CPU utilization of the program, here is simply the most basic method:


On Windows, you can bring up task Manager from the Ctrl+alt+del combination, and then view the details of the specified process.


Linux/android platform, you can enter the command line: Top command, find the specified process.


Note that when testing, there are usually several sets of conditions to test, such as: idle, medium load operation, high load operation ...


2. What are the factors that lead to higher CPU usage?


(1) Complex calculation


Run some algorithm processing, such as: Audio and video codec, image processing, scientific calculation and so on, especially some floating point number of operations.


(2) Continuous CPU consumption


Some high-priority processes/threads continue to occupy the CPU, rarely or never sleep, similar to while (1) {}


(3) Data copy


A large number of data copies are performed frequently.


(4) Frequent system calls


For example: frequently call printf print, read and write hard disk, network transceiver and so on.


3. What are the means to reduce CPU usage?


(1) Hardware acceleration


Common hardware acceleration is: Multi-core computing, GPU, DMA, audio and video hardware codec, and many hardware acceleration features require the platform to provide API or driver support, when the need to optimize the CPU occupancy rate can focus on.


(2) Learn to "give up" CPU


In the program, there are many ways to "give up" the CPU, the first is sleep, the second is the await/signal mechanism, any programming language will have a similar interface.


In addition, there is a strategy: to properly lower your process/thread priority.


(3) Avoid frequent copying of data


In multi-threaded programming, the data copy is unavoidable, but there are some techniques to reduce some unnecessary copies, the mind must have this idea, programming with more snacks.


(4) Merging some system calls


Many times, multiple prints can be merged together to print, multiple hard disk/network access requests can also be combined and sent together.


4. Summary


About the CPU occupancy of those things are introduced here, a lot of places just point to stop, and did not fully expand, we can search the relevant articles and materials to do in-depth optimization and analysis, have any questions or suggestions welcome message or letter [email protected] exchange, or follow my Sina Weibo @ Lu _ June or follow my public number (@Jhuster) to get the latest articles and information.


This article is from the "jhuster column" blog, be sure to keep this source http://ticktick.blog.51cto.com/823160/1723342

Practical development experience: How to reduce CPU usage

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.