将方法另存为变量

时间:2015-08-09 07:32:21

标签: java methods types

我试图将outOfBound r方法中多次调用的方法lengthOfColo保存到局部变量,以便使用较少的处理能力。我提供了lengthOfColor方法,我想在其中存储变量,我还提供了outOfBounds方法。正如您所看到的,outOfBounds方法是一个布尔值,我不知道如何使用整数参数存储它。

private Integer[] lengthOfColor(int col, boolean color, int pattern, int row) {
        int x = 0;
        int y = 0;
        if (pattern == 1) {
            // vertical pattern
            y = 1;
        } else if (pattern == 2) {
            // horizontal pattern
            x = 1;
        } else if (pattern == 3) {
            // diagonal slope left pattern
            x = 1;
            y = 1;
        } else {
            // diagonal slope right pattern
            x = 1;
            y = -1;
        }
        // length = how many neighbor slots are of same color
        // possible equals number of slots, that you can play off of.
        // whichSide = left or right if horizontal and top or bottom if vertical.
        int length = 0;
        int possible = 0;
        Integer[] whichSide = new Integer[]{1, -1}; 
        for (int side : whichSide) {
            int i = 1;
            boolean complete = false;
            //while complete is false continue the loop
            while (!complete) {
                //mainX == horizontal pattern distance
                //mainY == vertical pattern distance
                int mainX = x * i * side;
                int mainY = y * i * side;
                //if still inbounds and if the same slot is filled and it matches the color, increment length
                if (!outOfBounds(col, mainX, mainY, row) && getIsFilled(col, mainX, mainY, row) &&
                    checkColor(col, mainX, mainY, row) == color) 
                {
                    length++;
                } 
                //if still inbounds and if the same slot is empty, increment possible number of spots and change complete to true
                else if (!outOfBounds(col, mainX, mainY, row) && !getIsFilled(col, mainX, mainY, row) && 
                           getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY - row) 
                {
                    possible++;
                    complete = true;
                }
                //finish the statement to avoid a infinite loop if neither conditions are met.
                else
                {
                    complete = true;
                }
                // If not complete, then check one slot further.
                i = i + 1;
            }
        }
        return new Integer[] {length, possible};
    }


private boolean outOfBounds(int col, int x , int y, int row) 
    {
        int currentX = col;
        int currentY = getLowestEmptyIndex(myGame.getColumn(col)) - row;
        return currentX + x >= myGame.getColumnCount() || currentY + y >= myGame.getRowCount() || currentX + x < 0 || currentY + y < 0;
    }

1 个答案:

答案 0 :(得分:1)

我看到mainX和mainY改变了值,所以除了在调用if检查之前创建一个保存outOfBounds结果的布尔值之外,没有任何真正的优化可以在for和while循环之外完成。会减少你需要做的操作次数。说实话,优化是如此微不足道,以至于它并不重要但我认为是良好的编码实践(JIT可能会根据您的代码优化您)。更重要的是,该方法减少了您需要输入的额外代码行,并不一定意味着计算量较少。

在任何outOfBounds调用之前,在while循环中,

之类的东西
boolean outOfBounds = outOfBounds(col, mainX, mainY, row);

并将您当前的if(!outOfBounds(col, mainX, mainY, row) && ....)更改为if (!outOfBounds && ...)

优化的第一条规则是在完成项目之后不进行优化,并注意到性能显着下降。在这种情况下,您将从最大的瓶颈开始,直到获得最佳性能。当然,这并不意味着以不正确的方式编码,这当然会产生不必要的性能损失。在这些情况下,考虑您是否以正确的方式而不是微观优化来考虑问题也是明智之举。

以下是我将对显示的代码进行微观优化的内容。

private Integer[] lengthOfColor(int col, boolean color, int pattern, int row) { // consider changing Integer[] into
  // int[] if you don't need a boxed integer. It will increase performance
  int x = 0;
  int y = 0;
  // length = how many neighbor slots are of same color
  // possible equals number of slots, that you can play off of.
  // whichSide = left or right if horizontal and top or bottom if vertical.
  int length = 0;
  int possible = 0;
  switch (pattern) { // switch may be a tad faster but insignificant. More importantly it provides clarity.
    case 1:
      y = 1;
      break;
    case 2:
      x = 1;
      break;
    case 3:
      x = 1;
      y = 1;
      break;
    default:
      x = 1;
      y = -1;
      break;
  }
  //int[] whichSide = new int[]{1, -1}; // changed to int[] because you don't need a boxed primitive from what is
  // shown
  // nevermind, this line isn't needed and you will be able to avoid an instantiation.
  for (int i = 1; i != -3; i-=2) {
    int count = 1;
    int mainX; // bring this to a higher scope. (honestly this is micro optimization but a habit of mine if this is
    // can be considered in scope)
    int mainY;
    boolean outOfBounds = false;
    //boolean complete = false; // removed as its unnecessary to break out of the while loop.
    //while complete is false continue the loop
    while (true) {
      //mainX == horizontal pattern distance
      //mainY == vertical pattern distance
      mainX = x * count * i;
      mainY = y * count * i;
      outOfBounds = outOfBounds(col, mainX, mainY, row);
      //if still inbounds and if the same slot is filled and it matches the color, increment length
      if (!outOfBounds && getIsFilled(col, mainX, mainY, row) &&
      checkColor(col, mainX, mainY, row) == color) {
        length++;
      }
      //if still inbounds and if the same slot is empty, increment possible number of spots and change complete to
      // true
      else if (!outOfBounds && !getIsFilled(col, mainX, mainY, row) &&
      getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY -
      row) {
        possible++;
        break;
      }
      //finish the statement to avoid a infinite loop if neither conditions are met.
      else {
        break;
      }
      // If not complete, then check one slot further.
      count++;
    }
  }
  return new Integer[]{length, possible}; // once again consider whether or not you need a boxed integer
}


private boolean outOfBounds(int col, int x, int y, int row) {
  //int currentX = col; this is an unnecessary line
  int currentY = getLowestEmptyIndex(myGame.getColumn(col)) - row;
  return col + x >= myGame.getColumnCount() || currentY + y >= myGame.getRowCount() || col + x < 0 ||
  currentY + y < 0;
}