UNT0024 Give priority to scalar calculations over vector calculations
April 29, 2021 ยท View on GitHub
When working in tight loops or performance-critical sections, remember that scalar math is faster than vector math.
Therefore, whenever commutative or associative arithmetic allows, attempt to minimize the cost of individual mathematical operations.
You can see here, the related documentation on the Unity website.
Examples of patterns that are flagged by this analyzer
using UnityEngine;
class Camera : MonoBehaviour
{
public void Compute()
{
Vector3 x;
float a, b;
Vector3 slow = a * x * b;
}
}
Solution
Re-order operands for better performance:
using UnityEngine;
class Camera : MonoBehaviour
{
public void Compute()
{
Vector3 x;
float a, b;
Vector3 fast = a * b * x;
}
}
A code fix is offered for this diagnostic to automatically apply this change.