How to write code that is good for compiler optimization

Publisher:EE小广播Latest update time:2021-11-09 Source: EEWORLDAuthor: IAR SystemsKeywords:compiler Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


To emulate the behavior of prefix ++ for primitive types, operator++() can modify the object and return a reference to the modified object. What about emulating the behavior of postfix ++ for primitive types? Remember? "The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented." Just like the non-linear code above, the implementer of operator++(int) must copy the original object, modify the original object, and return the copy by value. Because of the copy operation, operator++(int) has a higher overhead than operator++().


For primitive types, the optimizer can often eliminate unnecessary copies if the result of i++ is ignored, but the optimizer cannot transform a call to one overloaded operator into another. If you write i++ instead of ++i out of habit, you are calling the more expensive increment operator.


Although we have been advocating against postincrement, we have to admit that postincrement is useful in some cases. If you really want to postincrement a variable, then go ahead. If the postincrement operation is consistent with what you expect, use it. But be careful not to use postincrement just to avoid writing an extra line of code to increment a variable.


Every time you add an unnecessary post-increment to a loop condition, an if condition, a switch expression, a ?:- expression, or a function call parameter, you force the compiler to generate larger, slower code. Is this list too long to remember? Start developing good habits today! Before using a post-increment, ask yourself if you can use the increment as the next statement.


Conclusion


Of course, software development does not only require developers to "make do" with compilers. The mutual cooperation between them and the compiler is one of the foundations for completing programming work quickly and efficiently. In addition, from the perspective of the development process of compilers, they must not only iterate and innovate with the evolution of technology and language, but also refer to more development habits. Compilers with a longer history and wider use can bring higher efficiency to developers.

Therefore, after understanding how to write code that is optimized by an excellent compiler, users can work more efficiently with less effort. The principles and tips mentioned in this article are also the best practices summarized by companies such as IAR Systems over a long period of time, and can be verified and explored in the company's Embedded Workbench. In its tool interface, you can view the execution time and code size of the code to find the best solution.

In addition to general code compilation optimization, good tools also support highly flexible custom optimization settings. For example, IAR Embedded Workbench includes different optimization levels for operating efficiency and code size. For different application requirements, you can also set the optimization level from the entire project to each source code file, and even each function, to help engineers adapt the best optimization solution for their applications. I hope this article will help developers to have a deeper understanding of program optimization.


[1] [2]
Keywords:compiler Reference address:How to write code that is good for compiler optimization

Previous article:STMicroelectronics updates TouchGFX software, adds video features to enrich STM32 user experience
Next article:Socionext develops LSI for next-generation cloud labeling to accelerate digital transformation in logistics

Recommended ReadingLatest update time:2024-11-16 15:26

Debugging Mini2440 with IAR
Using IAR to debug mini2440, ready to play naked running first When I was developing Lpc2214, I used IAR debugging environment, which was pretty good. I once misunderstood this development environment. Because Lpc can configure the Jtag pin as a normal IO I accidentally configured it as IO, which caused IAR to b
[Microcontroller]
SiRuiPu and IAR work together to build an embedded development ecosystem
IAR Embedded Workbench for Arm fully supports 3PEAK TPS32 mixed-signal microcontroller mainstream products Shanghai, China – January 18, 2024 – IAR, a global leader in embedded development software and services, and 3RuiPu today jointly announced that IAR’s flagship product IAR Embedded Workbench
[Embedded]
SiRuiPu and IAR work together to build an embedded development ecosystem
[IAR warning] Error[e16]: Segment INTVEC error
Error : Segment INTVEC (size: 0xec align: 0x2) is too long for segment definition. At least 0xac more bytes needed.    The problem occurred while processing the segment placement command "-Z(CODE)INTVEC=00-3F", where at the    moment of placement the available memory ranges were "CODE:0-3f"     Reserved ranges relev
[Microcontroller]
IAR+STM8_EXTI external IO interrupt
MCU: STM8S207SB IO port to be operated: PD7 uses falling edge to trigger interrupt Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 #include iostm8s207sb.h   #define LED1_FLASH PD_ODR_ODR3 = !PD_ODR_ODR3 // LED1 on the d
[Microcontroller]
static, const, code keywords in C51 development
static Keyword Static can be used to modify variables in C, and can also be used to modify functions. static modifier variable Variables can exist in three places in C: the global data area, the stack, and the heap. The stack and the heap are different. int a ; int main() {   int b ;   int c* = (int *)malloc(sizeo
[Microcontroller]
STM8 firmware library + IAR -- Environment
It's been quite a while since I got the board, but I've been busy with all sorts of things and haven't been able to properly practice with this board. Now... I'll just show you my development board, a three-in-one kit~~ I downloaded a basic program, no problem! But I saw that the stm8 on the board is STM8S207SB, but
[Microcontroller]
Calling printf() function in IAR based on STM32
Add the following code to the main file main.c: #ifdef __GNUC__     #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif   PUTCHAR_PROTOTYPE {       USART_SendData(EVAL_COM1, (uint8_t) ch);     while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) ==
[Microcontroller]
STM32 development board example explanation 1: Use IAR 5.3 to create a project
       Learning STM32, getting started is very important, and the first one you build yourself is also very important. Getting started as soon as possible and building up confidence in learning will be of great help to subsequent learning. This article will describe how to build a project on IAR and start your first ex
[Microcontroller]
Latest Embedded Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号