Arduino - 优化现有的数组迭代方法 [英] Arduino - Optimising existing method for iterating through an array

查看:28
本文介绍了Arduino - 优化现有的数组迭代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更高效、更简洁的方法来执行以下方法已经在执行的操作?

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.

Arduino - 高效地遍历 C 数组

推荐答案

应该不需要本地 arr 数组变量.如果你不这样做,你应该既节省临时堆栈空间,又通过消除复制数据的需要来加快执行速度.

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);
    }
  }
}

此代码假设存储在 int 中的最大整数大于 inArray 的最大大小(这似乎是合理的,因为原始代码基本上通过对 nLimit 使用 int).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆