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 ...
随机推荐
- 六. Python基础(6)--语法
六. Python基础(6)--语法 1 ● Python3中, Unicode转字节的方法 print(bytes("李泉", encoding = 'utf-8')) prin ...
- Cracking The Coding Interview 5.7
//An array A[1-n] contains all the integers from 0 to n except for one number which is missing. In t ...
- jdk8--stream并行流
stream的并行流要理解一个框架如下: 单线程,多线程和并行流对比 package com.atguigu.java8; import java.util.concurrent.ForkJoinPo ...
- 安装ubuntu不能引导win7
台式机安装了ubuntu导致进不了win7了,2系统在同一硬盘. win7引导需要bootmgr和boot文件夹中的文件,2个东东在winows引导分区根目录下. 我的笔记本安装windows系统分区 ...
- git相关知识点
git add 和 git stage 有什么区别: 工作区(Working Directory).暂存区(Stage)和历史记录区(History)以及转换关系不能少: git stage 是 gi ...
- <YaRN><Official doc><RM REST API's>
Overview ... YARN Architecture The fundamental idea of YARN is to split up the functionalities of re ...
- js基础 三种弹出框 数据类型
总结:js三个组成部分ES:语法DOM:对象模型 => 通过js代码与页面文档(出现在body中的所有可视化标签)进行交互BOM:对象模型 => 通过js代码与浏览器自带功能进行交互 引入 ...
- 【Python】xml 解析
1. XML:指可扩展标记语言,是一种标记语言,用于存储数据和传输数据,但没有像HTML那样具有预定义标签,需要程序猿自定义标签 2. XML的解析:读取XML数据结构中的某些信息,比如读取书的属性 ...
- Opencv-Python 图像透视变换cv2.warpPerspective
# -*- coding:utf-8 -*- import cv2 import numpy as np import sys img = cv2.imread('test.jpg') # cv2.i ...
- cat命令合并多个txt文件
cat是concatenate的缩写,意为串联,之前经常看到别人在用cat命令,没有细究 cat命令两个常用的用法是: cat file.txt能够将txt中的内容显示出来 cat file1.txt ...