public
override
void
CheckAvailableSteps(BlankPosition _blankPosition, CallBackDelegate _callback)
{
if(CanMoveUp(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Up);
if(CanMoveDown(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Down);
if(CanMoveLeft(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left);
if(CanMoveRight(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right);
if(CanMoveLeft2(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left2);
if(CanMoveRight2(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right2);
return;
}
}
{
if(CanMoveUp(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Up);
if(CanMoveDown(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Down);
if(CanMoveLeft(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left);
if(CanMoveRight(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right);
if(CanMoveLeft2(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left2);
if(CanMoveRight2(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right2);
return;
}
}
这是一个水平横放棋子的走法判断程序。可以看出来,我依次测试了一个水平棋子的六种走法是否可行。后来我认为可以进一步进行调优:比如说如果该棋子可以上移,那么其它五种移法就不用考虑了,肯定不行。类似如果该棋子可以左移两位,那么一定可以左移一位。这样我就可以减少判断的次数,进而提高程序运行的效率。所以我把程序改成了类似这个样子:
public
override
void
CheckAvailableSteps(BlankPosition _blankPosition, CallBackDelegate _callback)
{
if(CanMoveUp(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Up);
return;
}
if(CanMoveDown(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Down);
return;
}
if(CanMoveLeft2(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Left2);
// 此处添加MoveLeft代码并CallBack, 略
return;
}
if(CanMoveRight2(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Right2);
// 此处添加MoveRight代码并CallBack, 略
return;
}
if(CanMoveLeft(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left);
if(CanMoveRight(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right);
return;
}
}
{
if(CanMoveUp(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Up);
return;
}
if(CanMoveDown(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Down);
return;
}
if(CanMoveLeft2(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Left2);
// 此处添加MoveLeft代码并CallBack, 略
return;
}
if(CanMoveRight2(_blankPosition))
{
_callback(_newPosition, _newBlankPosition, MoveMethod.Right2);
// 此处添加MoveRight代码并CallBack, 略
return;
}
if(CanMoveLeft(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Left);
if(CanMoveRight(_blankPosition))
_callback(_newPosition, _newBlankPosition, MoveMethod.Right);
return;
}
}
结果猜怎么着?程序运行效率骤然降低,从原来的5.88秒一下子长到了6.23秒,这可不是一个小数。明明添加了一些return避免过多的运算,可系统效率反而下降,真不知道为什么?难道代码运行效率与代码行数还有关系?
放到“新手区”。我想原因可能有两个:1、此处的修改对效率影响太小以至于可以忽略不计。2、测试误差比较大。我已经放弃这种优化方法了。