Skip to content

Latest commit

 

History

History
45 lines (32 loc) · 998 Bytes

File metadata and controls

45 lines (32 loc) · 998 Bytes

UNT0024 Give priority to scalar calculations over vector calculations

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.