Avoiding post increase or decrease
When we write a loop, most of us will use post increase or decrease, but there is a better solution. See below examples, which one is the better one?
Example1:
uint8_t CalcParity1(uint8_t* data, uint8_t len)
{
uint8_t rt = ;
for (uint8_t i = ; i < len; i++)
{
if (*data++)
{
rt++;
} }
return rt;
}
Example 2:
uint8_t CalcParity2(uint8_t* data, uint8_t len)
{
uint8_t rt = ;
for (uint8_t i = ; i < len; ++i)
{
if (*data)
{
rt++;
}
++data; }
return rt;
}
The answer is Example 2. Why? Let’s check the assemble code which is based on IAR MSP430 (note that it can be different for different compiler).

There are 32 execution codes totally. But in Example 2, there is 24 execution codes. The example 1 has a less source code, but the example 2 has a better performance with better reading.

See another example 3 below.
uint8_t CalcParity3(uint8_t* data, uint8_t len)
{
uint8_t rt = ;
for (uint8_t i = len; i > ; --i)
{
if (*data)
{
rt++;
}
++data; }
return rt;
}
It is my prefer solution. In most situations, it has a better performance than example 2. But there are same between example 2 and example 3 after checking assemble code at IAR MSP430. The IAR compiler did a good job.

It is also the recommendation of TI ULP Advisor.
" line 77: remark #1544-D: (ULP 13.1) Detected loop counting up. Recommend loops count down as detecting zeros is easier.
I list the rule13.1 here.
Rule 13.1 Count down in loops
What it means
In MSP430 assembly code, a conditional branch based on comparing a variable/register against a non-zero value requires two instructions: compare and branch. However, when branching & comparing against zero, a specific instruction, BNE, can be used to perform both actions. This also holds true for a branch statement in C. Hence a counting down loop can reduce one instruction for each iteration of the loop when compared to a loop counting up.
Risks, Severity
A counting-up loop consumes one extra instruction for every iteration of the loop.
Why it is happening
A loop with an index counting up is detected in the code.
Remedy
- Use a loop that counts down whenever possible.
- Ensure that -o2 optimization level is selected in the compiler, or greater implements are included in the project settings to enable optimization for counting down loops.
Code Example
int i;
P1OUT |= 0x01; // Set P1.0 LED on
for (i = 5000; i>0; i--) // Count down loop
// In instead of: (i = 0; i <5000; i++)
{
/* Execute your application code */
}
Avoiding post increase or decrease的更多相关文章
- Oracle 10g Block Change Tracking特性
Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...
- Oracle 差异增量和累计增量备份
网址: http://www.eygle.com/digest/2009/04/oracle_rman_incremental_backup.html 在rman增量备份中,有差异增量和累积增量的概念 ...
- Garbage Collectors – Serial vs. Parallel vs. CMS vs. G1 (and what’s new in Java 8)
转自:http://blog.takipi.com/garbage-collectors-serial-vs-parallel-vs-cms-vs-the-g1-and-whats-new-in-ja ...
- 提高神经网络的学习方式Improving the way neural networks learn
When a golf player is first learning to play golf, they usually spend most of their time developing ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- Garbage Collectors - Serial vs. Parallel vs. CMS vs. G1 (and what's new in Java 8)--转
The 4 Java Garbage Collectors - How the Wrong Choice Dramatically Impacts Performance The year is 20 ...
- Spark RDD Transformation 简单用例(二)
aggregateByKey(zeroValue)(seqOp, combOp, [numTasks]) aggregateByKey(zeroValue)(seqOp, combOp, [numTa ...
- USB ISP(ICSP) Open Programmer < PWM ADC HV PID >
http://sourceforge.net/projects/openprogrammer/?source=navbar Open Programmer http://openprog.alterv ...
- Oracle 块修改跟踪 (Block Change Tracking) 说明
Block ChangeTracking 是Oracle 10g里推出的特性.官网对Block change tracking 的定义如下: Adatabase option that causes ...
随机推荐
- Xilinx 7 series FPGA multiboot技术的使用(转)
reference:https://www.cnblogs.com/chensimin1990/p/9067629.html 当升级程序有错误的时候,系统会启动golden bitstream 注意: ...
- 一次Web请求返回406原因与解决方案
ajax请求,响应信息返回的却是报错406,. 1.断点调试,进入对应处理方法,且得到正确信息返回到解析器.使用的是ssm,前端ftl 2.js将返回错误信息打出来,类似为: 百度406错误出现的原因 ...
- mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long'
mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long' 当使用mybatis进行传参的时候 ...
- Python 通过队列实现一个生产者消费者模型
import time from multiprocessing import Process,Queue #生产者 def producer(q): for i in range(10): time ...
- JAVA Collections.shuffle打乱列表
在JAVA中如果想打乱LIST的顺序可以调用Collections.shuffle()或者Collections.shuffle(List<?> list, Random rnd)方法. ...
- math、numpy、pandas NaN 判断
>> np.nan == np.nan False >> np.nan is np.nan True >> math.nan is np.nan False > ...
- 【转载】 5G+边缘计算,着眼可见的未来 【边缘计算】
原文地址: https://www.cnblogs.com/upyun/p/10641489.html ------------------------------------------------ ...
- zedboard开发板上移植opencv代码(立体匹配)
前言 公司要做立体匹配相关的项目,已有matlab和c++版本,可是不能做到实时显示立体信息,想要硬件实现实时,无奈本渣也是个硬件的新手,先按照实验室lyq同学的思路在zedboard开发板的纯ARM ...
- Shell #*/ 和 %/*
#!/bin/bash i="this/is/a/path.config" name=${i#*/} path=${i%/*} echo $name echo $path is/a ...
- Android Studio安卓导出aar包与Unity 3D交互
Unity与安卓aar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...