Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Referencing variables of different class and optimisation.

Discussion in 'Scripting' started by SurfWise, Aug 30, 2022.

  1. SurfWise

    SurfWise

    Joined:
    Dec 13, 2020
    Posts:
    5
    Question about optimisation:
    I have two classes, one class responsible for calculating physics, the other contains coefficients that physics uses for calculations.

    Code (CSharp):
    1. public static class Coefficients
    2. {
    3. public static float[] someCoefficients {1,2,3}
    4. }
    5. public class Physics : Mono{
    6. float[] localCopyOfCoeffs ;
    7. void Start{
    8. localCopyOfCoeffs = Coefficients.someCoefficients;
    9. }
    10. void CalculatePhysics()
    11.    {
    12.     //use coefficients for calculations
    13.    //Should I use
    14.     localCopyOfCoeffs
    15.     //or
    16.    Coefficients.someCoefficients;
    17.    }
    18. }
    Considering performance - is it much better to keep the local copy of coeffs (cache the reference) or should I just reference these coefficients directly in CalculatePhysics.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You won't gain anything with this "local copy" approach. Both
    someCoefficients
    and
    localCopyOfCoeffs
    are just references to the same object in memory.
     
  3. SurfWise

    SurfWise

    Joined:
    Dec 13, 2020
    Posts:
    5
    Ok, Thank You