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 ...
随机推荐
- Centos防火墙设置与端口开放的方法
Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld.所以,端口的开启还是要从两种情况来说明的,即iptables和firewalld.更多关于CentOs防火墙的最新 ...
- C++关于运算符的注意事项
1.函数调用也是一种特殊的运算符,对运算对象的个数不作限制. 2.几元运算符,是基于作用的对象的数量. 3.不同类型的运算对象进行运算,可能会出现类型转换,一般情况下小整数类型会被转换成较大的整数类型 ...
- 源码学习:一个express().get方法的加载与调用
刚刚接触express,它的中间件确实把我搞得头晕.get的回调中要不要加next?不加载还会执行下一个中间件么?给get指定'/'路径是不是所有以'/'开头的访问在没有确切匹配时都能执行?use件又 ...
- SQL-2 查找入职员工时间排名倒数第三的员工所有信息
题目描述 查找入职员工时间排名倒数第三的员工所有信息CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT ...
- box-shadow 边框阴影
box-shadow: 0 0 20px #000 inset;
- freemarker中的null异常处理以及!与??的使用(转)
原文链接: https://blog.csdn.net/mexican_jacky/article/details/50638062 阅读数:6304 如工程包含: 在user中我们有个角色,那么我们 ...
- VS2012里面使用EF框架的增删改查和分页的方法
public class BaseRepository<T> where T : class { //实例化EF框架 DataModelContainer ...
- Linux平台搭建-----C语言
下面内容是新手上路,各位高手路过勿喷!因为我第一次发布,可能页面设置或者其他做的不好,还请见谅~该文章只是作为我学习C语言的笔记以及记录学习进程的. 零基础学习C语言---搭建Linux平台开发环境 ...
- 通过调整浏览器UA设置欺骗限制上网
先上图片, 通过调整浏览器UA,欺骗识别,原来这个WIFI是只能手机端使用的,打开IE F12,进行如上图所示,进行修改,正常输入手机号,获取验证码,登陆后,即可上网了.虽然显示的是400,但实际 ...
- 关于“用VS2010的C++导入ADO导入不了,提示无法打开源文件msado15.tlh”的问题
vc++2010中,要使用ado操作数据库,所以在stdafx.h中引入了ado的dll库,引入代码如下: #import "C:/Program Files/Common Files/Sy ...