Newbie School: code optimization in embedded system programming

Source: Internet
Author: User
Article title: Newbie School: code optimization in embedded system programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Nowadays, with the high development of computer technology, embedded systems have been widely used in industrial control, information appliances, office automation, mobile communication, instrumentation, medical electronics, national defense, and other fields. With the further development and promotion of various embedded products at home and abroad, embedded technology is increasingly closely integrated with people's daily lives.

An embedded system is generally a non-PC system that provides one or more specific computer functions. It is a dedicated computer system centered on applications, with software and hardware cut-down and adapted to the functional, reliability, cost, volume, power consumption and other comprehensive requirements of the application system. In short, it is similar to the working mode of BIOS in a PC. it features low software code, high automation, and fast response speed. It is particularly suitable for application systems that require real-time and multi-task processing. Embedded real-time systems are currently one of the booming industries. However, the characteristics of real-time embedded systems make the software restricted by time and space, and the complex runtime environment makes the development of embedded system software very difficult. To design a secure and reliable high-performance embedded system that meets the functional, performance, and time requirements, programming language selection is very important.

1. programming language selection in embedded systems

Because the code written in assembly languages is difficult to understand, it is difficult to maintain and debug, and it can only be used in complex systems because of the poor portability of specific architectures and processors, it is not easy to realize Software Reuse. advanced languages have good versatility and rich software support, good portability and easy maintenance. Therefore, advanced language programming has many advantages. With the increasing application scope of embedded systems and the widespread use of Real-Time Operating System (RTOS), advanced language programming has become an inevitable trend in the design of embedded systems. However, it is not ruled out that some software modules are still written in assembly language, which can make the program more effective. Although the C/C ++ compiler optimizes the code, the appropriate use of inline assembly commands can effectively improve the efficiency of the entire system. Currently, many languages are used in the development of embedded systems, but only a few languages have been widely used. Among them, C and C ++ are the most widely used. C ++ has effectively improved C in supporting modern software engineering, OOP (Object Oriented Programming, object-oriented Programming), and structuring, however, in terms of program code capacity, execution speed, program complexity, and other aspects, the performance is worse than that of C language programs. C language is the most widely used programming language in embedded systems because it has both the ability to directly control hardware and the flexibility of advanced languages. With the development of network technology and embedded technology, Java is also widely used.

2. code optimization in real-time programming

In embedded system development, due to the demand for low-cost products, hardware designers need to provide enough storage and processing capabilities to complete their work. Therefore, in the last stage of embedded software design, code optimization is implemented.

The goal of code optimization is to reduce the size and speed, which can be considered from three aspects: Algorithm, data, and command flow.

Algorithm optimization

In most cases, the same speed as the memory (or performance, such as compression performance) is not a combination. At present, one of the common algorithms for program acceleration is to use the lookup table to avoid computation (for example, there is a huffman code table in jpg and a conversion table from YUV to RGB) in this way, the original complex computation is now only available for table queries. Although the memory is wasted, the speed is significantly improved. In addition, you should pay attention to improving efficiency when writing programs, for example:

Sort cases according to the frequency of occurrence in the Switch statement

The switch statement is a common programming technology. the compiler will generate the nested if-else-if code and compare it in sequence. when a match is found, the switch statement will jump to the statement execution that meets the conditions. Pay attention to it when using it. Every test and jump implemented by machine language only consumes valuable processor time to determine what to do next. To increase the speed, try to sort them according to their relative frequency. In other words, put the most likely and least possible situations at the end.

Convert large switch statements into nested switch statements

When there are many case labels in a switch statement, it is wise to convert a large switch statement into a nested switch statement to reduce the number of comparisons. Place the case label with a high occurrence frequency in a switch statement, which is the outermost layer of the nested switch statement. The case label with a relatively low occurrence frequency is placed in another switch statement. If a switch has a lot of work to do in each case, it is more effective to replace the entire switch statement with a table pointing to the function pointer.

Replace arrays with pointers

In many cases, array indexes can be replaced by Pointer operations, which often produces fast and short code. Compared with array indexes, pointers generally make code faster and consume less space. When using multi-dimensional arrays, the time difference is more obvious. The following code serves the same purpose, but the efficiency is different.

Array index pointer operation

For (;) {p = array

A = array [r ++]; (;;){

A = * (p ++ );

............

}}

[1] [2] Next page

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.