- Home /
Invisible method costs?
Hello
I'm profiling some performance critical code and try to find bottlenecks.
I've found a bottleneck which I don't understand and need your help.
Using the profiler, I've found that the following method uses 2.43ms itself (excluding sub method calls) for just 625 calls.
The method itself only does basic opperations like loop, comparision, assignement and variable declaration, so I don't see a real reason for this huge impact.
Be aware that the list "lodSources" usually consists of 1 or 2 items.
Can someone point me to the part of the code which could be using a lot of processor time?
What I have tried so far:
Making method non static
Using different collections for the lodSource argument
Replacing if(a<b){a=b;} with a=Math.Min(a,b);
Code
protected static void UpdateLodObjectDistance(LodObjectBase lodObjectBase, List<LodSource> lodSources)
{
// The shortest distance found
var relativeDist = float.PositiveInfinity;
var absoluteDist = float.PositiveInfinity;
var position = lodObjectBase.Transform.position;
for (var i = 0; i < lodSources.Count; i++)
{
float realDist;
// Get the distance (already multiplied with the distance multiplier when needed)
var dist = lodSources[i].GetDistanceToObject(position, out realDist);
if (dist < relativeDist)
{
relativeDist = dist;
}
if (realDist < absoluteDist)
{
absoluteDist = realDist;
}
}
// Apply the new distances to the lod object
lodObjectBase.SetNewRelativeDistance(relativeDist);
lodObjectBase.SetNewAbsoluteDistance(absoluteDist);
}
Profiler result
Edit:
Using Windows 7
Unity 5.3.2f1
Profiler uses Deep Profile
Thank you
Chillersanim
What is that "lodSources[i].GetDistanceToObject(position, out realDist);" doing?
Also for micro-optimization, just use the (a < b) style for $$anonymous$$s, absolute values, etc. Like it or not, every function call has overhead (not enough to cause 37ms of calculations in this case though),
The LodSource.GetDistanceToObject() calculates the distance from the LodSource to an object. As there's a more complex algorithm behind it (based on view angle, etc.), a simple Vector.Distance(a,b) is not enough.
This method has some computation cost, but it isn't part of the 21.4% respective 2.43ms the method itself uses.
But it's being called 625 times each. Can you link / micro-optimize that inner function?
$$anonymous$$y concern is not that inner method.
$$anonymous$$y concern is that Unity tells me, that my method has a "self" cost of 21.4%, meaning that the total cost $$anonymous$$us the cost for all inner method calls is very high for such a simple method.
The total cost is inclusive the 11.1% of the inner method GetDistanceToObject(), I probably could micro optimize it, but the slow down is not happening there (when I can believe the profiler...).
Please correct me when I misunderstood something, but that's how I understand the profiler numbers.
Thank you.
I'll try doing the build debug and let you know if that helped. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
CPU being choked by GC.MarkDependencies 1 Answer
Distribute terrain in zones 3 Answers
Intermittent slow downs in Unity - unrelated to garbage collector 1 Answer
Huge "other" in Gpu Profile ! 4 Answers