Skip to main content
Move attack range test after weakest/lowest ID test
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

The tests c.HP > 0 and c.Team != Team are probably blazingly fast tests. Location.Dist(c.GetLocation()) <= AttackRange probably involves the square-root of the sum of the squares of the difference of coordinates in two or three dimensions. Plus, GetLocation() may involve memory allocation and/or copying constructors. It is by far the slowest test, yet you are testing it first! Take advantage of the short-circuit logical and operators by reordering the tests so the fastest tests are done first, so the slowest test may not even need to be executed, resulting in faster execution.

Since all conditions must pass before you update weakestEnemy, you could even test whether or not the target passes the “weakest with lowest ID” test before checking the attack range.

Bonus: the square-root can be avoided; simply compute the square distance, and compare against the \$AttackRange^2\$ (computed outside of the loop) for another speed gain.

The tests c.HP > 0 and c.Team != Team are probably blazingly fast tests. Location.Dist(c.GetLocation()) <= AttackRange probably involves the square-root of the sum of the squares of the difference of coordinates in two or three dimensions. Plus, GetLocation() may involve memory allocation and/or copying constructors. It is by far the slowest test, yet you are testing it first! Take advantage of the short-circuit logical and operators by reordering the tests so the fastest tests are done first, so the slowest test may not even need to be executed, resulting in faster execution.

Bonus: the square-root can be avoided; simply compute the square distance, and compare against the \$AttackRange^2\$ (computed outside of the loop) for another speed gain.

The tests c.HP > 0 and c.Team != Team are probably blazingly fast tests. Location.Dist(c.GetLocation()) <= AttackRange probably involves the square-root of the sum of the squares of the difference of coordinates in two or three dimensions. Plus, GetLocation() may involve memory allocation and/or copying constructors. It is by far the slowest test, yet you are testing it first! Take advantage of the short-circuit logical and operators by reordering the tests so the fastest tests are done first, so the slowest test may not even need to be executed, resulting in faster execution.

Since all conditions must pass before you update weakestEnemy, you could even test whether or not the target passes the “weakest with lowest ID” test before checking the attack range.

Bonus: the square-root can be avoided; simply compute the square distance, and compare against the \$AttackRange^2\$ (computed outside of the loop) for another speed gain.

Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

The tests c.HP > 0 and c.Team != Team are probably blazingly fast tests. Location.Dist(c.GetLocation()) <= AttackRange probably involves the square-root of the sum of the squares of the difference of coordinates in two or three dimensions. Plus, GetLocation() may involve memory allocation and/or copying constructors. It is by far the slowest test, yet you are testing it first! Take advantage of the short-circuit logical and operators by reordering the tests so the fastest tests are done first, so the slowest test may not even need to be executed, resulting in faster execution.

Bonus: the square-root can be avoided; simply compute the square distance, and compare against the \$AttackRange^2\$ (computed outside of the loop) for another speed gain.