Some Ideas about while (1) and for (;) Efficiency

Source: Internet
Author: User

In the past few days, I have seen my friend's article about the efficiency of while (1) and. When you look back, you only need to judge. No matter what the experiment is, let's look at the differences between the two writing methods:


Lab environment: IAR ewarm 5.2

I just made an article on an embedded project. First, the projectc language compilation optimization selects none, and the output selects Assembly output. The output Assembly file corresponds to the C language.




I used for (;) in the main function to write an endless loop. Let's look at the compilation result:

Note that main. c generates a corresponding main. s




We can see that for (;) is directly replaced by a jump command (;;).


Change the Code:



Compile to see the result:


The while (1) loop is also replaced by a jump command.


Under IAR ewarm 5.2, we can see that for (;) and while (1) are exactly the same under the optimization code level. No matter what the difference.


Let's take a look at the results of the arm-rtems4.9-gcc in Linux:



I wrote a very easy code:



First, let's look at the compilation result of while (1:


Enter the following commands in the graph:






As you can see, the while (1) loop has become a B. l7, and no inference is made on 1. It's just one command.


Let's take a look at (;;)




Enter the following command:



The result is as follows:


The two results are the same. In the same way, I didn't turn on Optimization for GCC. If you need to look at the optimization compilation results, you can use gcc-O2-s test. c. You can also use gcc-OS-s test. c,-OS is much more used in embedded systems. Code without optimization is the same, let alone optimized code.


In fact, this is a tough topic. C/C ++ is flexible in writing, and compiler optimization is very different and has its own characteristics. This reminds me of the C language programming written by Mr. Tan haoqiang. From the very beginning, I didn't think how this book was, but I couldn't write any better books. It is certain that there are a large number of writing statements in the book ++ I, I ++, and highly relevant to the running results and compiler. Harmful. The running result of another compiler is different. It's really wrong. But this is also an indisputable fact, that is, standardized writing can avoid the risk of porting in multiple compilers.


For the discussion on the efficiency of for (;) and while (1), I personally think:

1. There is no difference between the two writing methods, which is highly related to the compiler. This is something we have the ability to care about, not the ability to care about, and not the need to care too much;

2. embedded code for C/C ++ writing requirements are very high, it is recommended that have basic friends read MISRA-C2004, 2008 and some C ++ international standards;

3. Focus on code specifications rather than code efficiency. After all, the single-chip microcomputer also makes it fast enough, and most of the cases are true. If you are a friend of code optimization or algorithm, I suggest you read the book "The Art of computer programming, better understanding of an assembly language. It will be of great use.



Some Ideas about while (1) and for (;) Efficiency

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.