Efficient C Tips #7 – Fast loops
来源:互联网 发布:c语言输出文件名 编辑:程序博客网 时间:2024/05/22 14:41
by Nigel Jones
原文: http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-7-fast-loops/
Every program at some point requires some set of actions to be taken a fixed number of times. Indeed this is such a common occurrence that we typically code it without really giving it much thought. For example, if I asked you to call a function foo() ten times, I’m sure that most of you would write something like this:
for (uint8_t lpc = 0; lpc < 10; ++lpc){ foo();}
While there is nothing wrong with this, per se, it is sub optimal on just about every processor. Instead you are better off using a construct which counts down to zero. Here are two alternative ways of doing this:
for(uint8_t lpc = 10; lpc != 0; --lpc){ foo();}uint8_t lpc = 10;do{ foo();} while (--lpc);
Which one you think is more natural is entirely up to you.
So how does this efficiency arise? Well in the count up case, the assembly language generated by the compiler typically looks something like this:
INC lpc ; Increment loop counterSUB lpc, #10 ; Compare loop counter to 10BNZ loop ; Branch if loop counter not equal to 10
By contrast, in the count down case the assembly language looks something like this
DEC lpc ; Decrement loop counterBNZ loop ; Branch if non zero
Evidently, because of the ‘specialness’ of zero, more efficient code can be generated.
So why don’t you see C programs littered with these count down constructs? Well counting down has a major limitation. If you need to use the loop variable as an index into an array then you have a problem. For example, let’s say I wanted to zero the elements of an array. Using the count down technique you might be tempted to do this:
uint8_t bar[10];uint8_t lpc;do{ bar[lpc] = 0; // Error! First time through results in index beyond end of array} while (--lpc);
Evidently it doesn’t work. You can of course modify the code to make it work. However doing so typically loses you all the efficiency gains, such that you are better off with a standard up-counting for loop.
As a parting thought, concepts such as these are second nature to assembly language programmers – all of whom do this sort of thing instinctively. As a result, if you are really interested in getting the best out of your C compiler, you could do a lot worse than learning how to program your target processor in assembly language. Does this defeat one of the objectives in programming in a high level language – yes. However, for giving you insight in terms of what is going on under the hood it cannot be beaten.
// 原文评论也很精彩.
- Efficient C Tips #7 – Fast loops
- Efficient C Tips #8 – Use const
- Efficient C Tips #1 – Choosing the correct integer size
- Efficient C Tips #2 – Using the optimizer
- Efficient C Tips #3 – Avoiding post increment / decrement
- Efficient C Tips #4 – Use Speed Optimization
- Efficient C Tips #5 – Make ‘local’ functions ‘static’
- Efficient C Tips #9 – Use lookup tables
- Efficient C Tips #10 – Use unsigned integers
- Efficient C Tips #6 – Don’t use the ternary operator
- Writing Fast, Memory-Efficient JavaScript
- Good tips for being efficient
- Make your Android Listview Fast and Efficient
- Make your Android Listview Fast and Efficient
- [转载]Writing Fast, Memory-Efficient JavaScript
- Write fast and efficient PLSQL - DBMS_PROFILER
- GDC2011: Fast and Efficient Facial Rigging
- Tips for writing efficient Python code
- 编程之美 读书笔记(寻找最大的k个数)
- DFD需求分析 具体方法步骤
- 移植libnfc到arm 开发板 ( porting libnfc to arm)
- retain和copy还有assign的区别
- 组件
- Efficient C Tips #7 – Fast loops
- 具体数学 第一章
- jsp与树形菜单
- 2011.09.12_BigPipe学习研究
- 【模拟】我要清晰的Unicode转码表
- servlet web.xml 中文乱码
- qt使用外部库
- 推荐算法分类
- hog lbp pedestrian