Quick optimization question - if I have a TArray that rarely changes size, but the size of the array is referenced very frequently, is it better to store the length of the array in a separate integer variable or call the Num() function very frequently?
I’m not sure whats going on internally in the Num() function. Does Num() just return a integer value saved somewhere memory? Or is it doing some computation to get the return value?
To give a code example - is the usage of Num() here bad practice or should I not worry about it:
UPROPERTY()
TArray<UPerson*> Population;
// ...
bool UResourceSimulator::SimulateFrequentGlobalEvents() {
if (Population.Num() == 0) {
return false;
}
FoodStock -= Population.Num();
WaterStock -= Population.Num();
UE_LOG(LogSS, Log, TEXT("applying global event | Food: -%d, Water -%d"), Population.Num(), Population.Num());
return true;
}