Hello everyone,
Im 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.
using UnityEngine;
public class StuffMaker : MonoBehaviour
{
private Triangle[] myArray;
void Start ()
{
Triangle = new Triangle[1000];
}
void DoStuff (int i, int j)
{
float distance = Vector2.Distance(myArray[i].centerPosition, myArray[j].centerPosition);
// Do some stuff with that distance value.
}
}
public class Triangle
{
public Vector2 centerPosition;
}
Or should this be done instead?
using UnityEngine;
public class StuffMaker : MonoBehaviour
{
private Triangle[] myArray;
void Start ()
{
Triangle = new Triangle[1000];
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 3; j++) {
myArray[i].distance[j] = Vector2.Distance(myArray[i].centerPosition, myArray[j].centerPosition);
}
}
}
void DoStuff (int i, int j)
{
float distance = myArray[i].distance[j];
// Do some stuff with that distance value.
}
}
public class Triangle
{
public Vector2 centerPosition;
public float[] distance;
}
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!