Symptoms:
- Not all user code is shown in the Profiler, and Deep Profiling slows down my application.
Cause:
The built-in Profiler is not profiling all method calls. Also, Deep Profiling causes a large overhead that will significantly slow down your application execution. It may not even be possible to perform the profiling activity (Unity can run out of memory).
Resolution:
Profiler Sample is a block of code that starts with
Profiler.BeginSample()
and ends with Profiler.EndSample()
calls. Just like this:using UnityEngine; using UnityEngine.Profiling; public class NeedsProfiling : MonoBehaviour { void Update() { Profiler.BeginSample("My Sample"); Debug.Log("This code is being profiled"); Profiler.EndSample(); } }
You would put it before, and after the chunk of code you want to be profiled. Profiler Sample will record the execution time for you and is displayed in the Profiler window without using Deep Profiling.

Of course, you can add as many Profiler Samples as you want. Don’t worry about adding too many samples. These calls have zero overhead when they are deployed in a non-development build.
More Information
Comments
3 comments
Using ProfilerMarker instead of Profiler.BeginSample/EndSample comes with less overhead for profiling and can be used in a using scope to avoid missing an EndSample call when returning earlier in the block or an exception getting thrown.
What is the user statement required? I get an error when I try to compile the code:
Manjit Bedi
ProfilerMarkers require a
to include the namespace they reside in, or
For more details please see the scripting documentation page for ProfilerMarkers.
Please sign in to leave a comment.