Arduino的 - 优化现有方法通过遍历数组 [英] Arduino - Optimising existing method for iterating through an array
问题描述
有没有做什么,下面的方法已经做的更有效和更清洁的方式?
Is there a more efficient and cleaner way of doing what the following method is already doing?
void sendCode(prog_uint16_t inArray[], int nLimit) {
unsigned int arr[nLimit];
unsigned int c;
int index = 0;
while ((c = pgm_read_word(inArray++))) {
arr[index] = c;
index++;
}
for (int i = 0; i < nLimit; i=i+2) {
delayMicroseconds(arr[i]);
pulseIR(arr[i+1]);
}
}
这是在参考现有的问题,我已经回答。
This is in reference to an existing question I had answered.
<一个href=\"http://stackoverflow.com/questions/8939937/arduino-iterate-through-c-array-efficiently/8951798#8951798\">Arduino - 遍历数组c有效
推荐答案
有应该不需要本地改编
数组变量。如果你与做掉,你都应该保存临时堆栈空间,并通过消除需要复制的数据加速执行。
There should be no need for the local arr
array variable. If you do away with that you should both save temporary stack space and speed up execution by removing the need to copy data.
void sendCode(const prog_uint16_t inArray[]) {
unsigned int c;
for (int i = 0; c = pgm_read_word(inArray++); i++) {
if (i % 2 == 0) { // Even array elements are delays
delayMicroseconds(c);
} else { // Odd array elements are pulse lengths
pulseIR(c);
}
}
}
这code假定存储在 INT
比 inArray
(这似乎合理,因为原来的code。通过使用基本上使相同的假设一个 INT
为 nLimit
)
This code assumes that the maximum integer stored in an int
is greater than the maximum size of inArray
(this seems reasonable as the original code essentially makes the same assumption by using an int
for nLimit
).
这篇关于Arduino的 - 优化现有方法通过遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!