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的更多相关文章

  1. Oracle 10g Block Change Tracking特性

    Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...

  2. Oracle 差异增量和累计增量备份

    网址: http://www.eygle.com/digest/2009/04/oracle_rman_incremental_backup.html 在rman增量备份中,有差异增量和累积增量的概念 ...

  3. 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 ...

  4. 提高神经网络的学习方式Improving the way neural networks learn

    When a golf player is first learning to play golf, they usually spend most of their time developing ...

  5. Chapter 6 — Improving ASP.NET Performance

    https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...

  6. 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 ...

  7. Spark RDD Transformation 简单用例(二)

    aggregateByKey(zeroValue)(seqOp, combOp, [numTasks]) aggregateByKey(zeroValue)(seqOp, combOp, [numTa ...

  8. USB ISP(ICSP) Open Programmer < PWM ADC HV PID >

    http://sourceforge.net/projects/openprogrammer/?source=navbar Open Programmer http://openprog.alterv ...

  9. Oracle 块修改跟踪 (Block Change Tracking) 说明

    Block ChangeTracking 是Oracle 10g里推出的特性.官网对Block change tracking 的定义如下: Adatabase option that causes ...

随机推荐

  1. JDK(java se development kit)的构成

    1.javac(Java compiler)编译器 通过命令行输入javac命令调用Java编译器,编译Java文件的过程中,javac会检查源程序是否符合Java的语法,没有语法 问题就会将.jav ...

  2. WebView加载页面

    //使用内置浏览器webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoa ...

  3. 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进行传参的时候 ...

  4. powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.

    powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model ...

  5. shell开源跳板机sshstack

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...

  6. ios九宫格算法

    - (void)viewDidLoad { [super viewDidLoad]; //1.总列数(一行最多3列) ; CGFloat appW=; CGFloat appH=; //2.间隙=(控 ...

  7. 常见无线DOS攻击

    记录下自己最近一段时间对无线渗透学习的笔记. 无线DOS就是无线拒绝服务攻击.主要包括以下几种攻击类型:Auth Dos攻击.Deauth Flood攻击.Disassociate攻击及RF干扰攻击等 ...

  8. Maven Speed Up

    收录架构 proxy代理仓库 不支持仓库搜索功能 收录版本 所有版本 更新时间 每24小时更新一次 使用说明 一.在maven软件中使用 以Maven 3.5.2为例: 打开maven配置文件 ./a ...

  9. 将numpy array由浮点型转换为整型

    使用numpy中的astype()方法可以实现,示例如下: x Out[20]: array([[ 5. , 4. ], [ 4. , 4.33333333], [ 3.66666667, 4.5 ] ...

  10. python socket 编程之三:长连接、短连接以及心跳(转药师Aric的文章)

    长连接:开启一个socket连接,收发完数据后,不立刻关闭连接,可以多次收发数据包. 短连接:开启一个socket连接,收发完数据后,立刻关闭连接. 心跳:长连接在没有数据通信时,定时发送数据包(心跳 ...