I had a look at the CPU profiler in Unity today as prep for my new job. Below is a super quick overview of the tool.
To open the profiler, go to Window > Profiler. This opens a new window showing a timeline of frames and a list of functions for each frame.
To view the current frame, click the current button in the top right. To view a specific frame, click on it in the graph. This will pause the game and give you information for that frame. The total ms cost for the frame is shown in the middle of the window. The lines on the graph show some target ms costs such as 33 and 16.
The list of functions allow us to see the time in ms for each function, as well as the percentage of the total frame time that that function uses. The self version of these shows the cost of the function without its children. The calls column shows how many calls to that function happen in the frame.
GC alloc shows how much garbage collection happens for that resource. Garbage is memory set aside to store data that is no longer in use – GC refers to the process that frees up this memory for use. If this happens too often it can hit performance, so worth keeping an eye on.
To get more information on child functions, we can run deep profiling by using the button next to record at the top of the window. Below, I’ve ran it to get more information on culling. Without deep profiling, I could see that this cost was related to culling lights, but with it on, I can see that its specifically linked to single directional shadows (common sense should have told me that lighting culling cost was associated with shadow casting, but data is always good!).
The profiler has a bit of an overhead – so make sure that you use the statics view with the profiler off to get the actual fps and ms per frame cost before profiling. Any logging functions that you may be using are part of debugging will also be fairly heavy.