The optimization of C + + code in embedded real-time programming

Source: Internet
Author: User
Tags arrays execution function definition

1 Introduction

With the rapid development of computer technology and information technology, the embedded computers of computers and computer technology are widely used in people's daily life. Embedded computers refers to the completion of one or more specific functions of the computer system, hardware and software is a close combination. It has the characteristics of small software code, high automation, fast response and so on. It is particularly suitable for applications that require real-time and multitasking.   Embedded real-time system is one of the flourishing industries at present. However, the characteristics of real-time embedded system make its software be restricted by time and space, coupled with complicated running environment, which makes the development of embedded system software extremely difficult. In order to design a system that satisfies the requirements of function, performance and dead-line, it is very important to develop the language selection in order to develop a high performance embedded system with safety and reliability.

The choice of language in 2 embedded real-time programming

With the expanding application scope of embedded system and the extensive use of embedded real-time operating system RTOs (real time operating system), advanced language programming is an inevitable trend of embedded system design. Because the assembly language and the specific microprocessor hardware structure is closely related, the transplant is poor, neither should be used in the complex system, but also inconvenient to realize the software reuse, but the high-level language has the good versatility and the rich software support, facilitates the promotion, is easy to maintain, therefore the high-level language programming has many advantages. At present, there are many kinds of languages used in the development of embedded system, but only a few languages have been widely used. of which C is the most widely used. C + + in support of modern software engineering, OOP (Object oriented programming, object-oriented programming), structured and other aspects of the effective improvement in the, but in the program code capacity, execution speed, program complexity and other aspects than the C language program performance. C language is the most widely used programming language in embedded system because of its ability of direct control of low-level language and the flexibility of advanced language. With the continuous development of network technology and embedded technology, the application of Java has been widely used.

3 C + + code optimization in real-time programming

While making the software right is a logical final step for the project, it is not always the case in embedded system development. In the demand for low-cost products, hardware designers need to provide just enough memory and the ability to handle the work. So in the last phase of embedded software design, it becomes the optimization of code.

Modern C and C + + compilers provide some degree of code optimization. However, most optimizations performed by the compiler only involve a balance between execution speed and code size. Your program can be faster or smaller, but it can't be faster and smaller. After I am in the embedded system design and implementation process practice, the following introduces several simple and effective C + + code optimization methods.

(1) Inline function

In C + +, the keyword inline can be added to any function declaration. This keyword requests the compiler to replace all calls to the indicated function with the code inside the function. Doing so is faster than a function call in two aspects. This is done in two aspects faster than the function call: first, it eliminates the execution time required by the calling instruction, and secondly, eliminates the time required to pass the variable and transfer process. But by using this method, the program is growing at a faster rate, requiring more ROM. Using this optimization is most effective when the inline function is called frequently and contains only a few lines of code.

(2) using pointers instead of arrays

In many cases, you can use pointer arithmetic instead of an array index, which often produces fast and short code. Pointers generally make code faster and occupy less space than array indexes. The differences are more pronounced when using multidimensional arrays. The following code works the same, but the efficiency is not the same.

Array index pointer operations

For(;;){         p=array
   A=array[t++];      for(;;){
                 a=*(p++);
   ......           ......
   }            }

The advantage of the pointer method is that the address of the array is only incremented to the P increment per loop after each load of address p. In the array indexing method, the complex operation of an array subscript based on t value must be performed in each loop.

(3) Undefined return value not used

The function function definition does not know whether the return value of the functions is used, and if the return value is never used, you should use void to explicitly declare that the function does not return any values.

Related Article

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.