• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chillersanim · Mar 29, 2016 at 10:32 AM · c#performanceprofiler

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

alt text

Edit:

  • Using Windows 7

  • Unity 5.3.2f1

  • Profiler uses Deep Profile

Thank you
Chillersanim

profiler-udatelodobjectdistance-29032016.png (115.6 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Mar 29, 2016 at 11:43 AM 0
Share

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),

avatar image chillersanim · Mar 29, 2016 at 11:50 AM 0
Share

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.

avatar image TreyH · Mar 29, 2016 at 12:10 PM 0
Share

But it's being called 625 times each. Can you link / micro-optimize that inner function?

avatar image chillersanim · Mar 29, 2016 at 12:14 PM 0
Share

$$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.

avatar image chillersanim · Mar 29, 2016 at 12:22 PM 1
Share

Thank you.

I'll try doing the build debug and let you know if that helped. :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

CPU being choked by GC.MarkDependencies 1 Answer

Multiple Cars not working 1 Answer

Intermittent slow downs in Unity - unrelated to garbage collector 1 Answer

Unaccounted time between: WaitForTargetFPS and GPUProfiler.EndQueries 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges