Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Storing values vs recalculating them.

Discussion in 'Scripting' started by MathiasDG, Nov 21, 2014.

  1. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Hello everyone,
    I`m working on some code and i`ve been wondering if it`s better to store as much values as i can in a class, to do less math later on, or to just store as few as possible, and do a lot of math later on?

    So here is an example of what i mean :
    I have Triangles. Should i store only their center positions, and then calculate their distance to their neighbors everytime i need, or should i store their distance and never have to recalculate that again?
    The positions do not ever change.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class StuffMaker : MonoBehaviour
    4. {
    5.         private Triangle[] myArray;
    6.  
    7.         void Start ()
    8.         {
    9.                 Triangle = new Triangle[1000];
    10.         }
    11.  
    12.         void DoStuff (int i, int j)
    13.         {
    14.                 float distance = Vector2.Distance(myArray[i].centerPosition, myArray[j].centerPosition);
    15.                 // Do some stuff with that distance value.
    16.         }
    17. }
    18.  
    19. public class Triangle
    20. {
    21.         public Vector2 centerPosition;
    22. }
    Or should this be done instead?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class StuffMaker : MonoBehaviour
    4. {
    5.         private Triangle[] myArray;
    6.  
    7.         void Start ()
    8.         {
    9.                 Triangle = new Triangle[1000];
    10.                 for (int i = 0; i < 1000; i++) {
    11.                         for (int j = 0; j < 3; j++) {
    12.                                 myArray[i].distance[j] = Vector2.Distance(myArray[i].centerPosition, myArray[j].centerPosition);
    13.                         }
    14.                 }
    15.         }
    16.  
    17.         void DoStuff (int i, int j)
    18.         {
    19.                 float distance = myArray[i].distance[j];
    20.                 // Do some stuff with that distance value.
    21.         }
    22. }
    23.  
    24. public class Triangle
    25. {
    26.         public Vector2 centerPosition;
    27.         public float[] distance;
    28. }
    In other words, is it better to store some value, or to recalculate it every time i need it?
    The DoStuff () function is called quite a lot but there are also a lot of triangles.

    Thank you very much!
     
    Last edited: Nov 21, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    This seems somewhat like 'premature optimization', worrying yourself over some 'non-critical' part of your program. You spend a long time on this, and make no real progress.
    The typical advice is to just do what works, then use the profiler later to see where you can save cycles.

    There's no doubt that pre-computing large data sets is beneficial, but is your situation big enough to make a difference? You can only know once the rest of your program is up and running