>
>
>
V4006. Unity Engine. Multiple operation…


V4006. Unity Engine. Multiple operations between complex and numeric values. Prioritizing operations between numeric values can optimize execution time.

The analyzer has detected an opportunity to optimize a mathematical operation, which can enhance performance if the operation is performed frequently.

Look at the synthetic example:

[SerializedField] float _speed;

void Update()
{
  ....
  Vector3 input = ....;
  var move = input * _speed * Time.deltaTime;
  ....
}

In the 'Update' method, the value of the 'move' variable, which is a displacement vector of a character per one frame, is calculated. This calculation involves six multiplication operations because when 'Vector3' is multiplied by a number, each of its three components (x, y, z) is multiplied by the same number. The number of 'Update' calls is not constant, it depends on the frame rate per second. Let's say that in our case, on average, this method is called 60 times per second. Then, it would take 60 * 6 = 360 operations per second just to calculate the displacement.

You can reduce this value by multiplying the simple numbers with each other and then multiplying the result by the vector:

var move = input * (_speed * Time.deltaTime);

Now, for a single displacement calculation, four multiplication operations are performed in one 'Update' call. That is 240 operations per second.

Note that the more simple numbers are included in such an expression, the more noticeable is the effect of the optimization.